需要帮助来完成以下代码,以便在MVC中使用SelectListItem进行简单的下拉列表 [英] Need help to complete the below code for a simple drop-down using SelectListItem in MVC
问题描述
我想创建一个显示国家/地区列表的简单下拉框。它的数据来自数据库,使用实体框架数据上下文进行访问。用户应该在发回数据之前选择一个国家。(简单验证检查)。
我已经创建了视图模型并且还编写了一些代码,但是我我不确定我的设计,我也需要帮助来完成代码。我做了一些搜索,但我找不到一个简单的方法。请帮忙。谢谢
查看模型 - Country.cs
public class 国家/地区
{[必填]
public int CountryId { get ; set ; }
public IEnumerable< SelectListItem>国家/地区{获取; set ; }
}
控制器
public ActionResult CountriesDropDown()
{
using (ctx)
{
var dropdown =(来自 q ctx .countries
选择 new Models.Country
{
Id = q.Id,
// 需要使用数据库填写国家/地区名称的国家/地区属性。
}
return view();
}
[HttpPost]
public ActionResult AddCountry(Models.Country country)
{
使用(ctx)
{
}
return view();
}
国家/地区视图
@ model Models.Country
@ Html.DropDownListFor(model => model.Country ..?..这里也需要代码,请选择)
@Html .ValidationFor(...此处需要代码以确保用户选择国家/地区)
您也可以通过其他方式执行此操作,按照这一个
public class 国家
{[必需]
public int CountryId {获得跨度>; set ; }
public string CountryName { get ; set :}
}
控制器
public ActionResult CountriesDropDown()
{
使用(ctx)
{
var dropdown =( from q in ctx.Countries
选择 new Models.Country
{
Id = q.Id,
Name = q,CountryName
// 需要使用国家/地区名称填充国家/地区属性的代码来自数据库。
}
Viewbag.Country =下拉列表;
return view();
}
国家查看
@ model Models.Country
@ Html.DropDownListFor(model = > model.CountryId, new SelectListItem {@ Viewbag.Country,CountryName,CountryId}, 请选择< /跨度>);
I would like to create a simple drop-down box that displays a list of countries. The data for it comes from the database and is accessed using the entity framework data context. The user should select a country before posting the data back.(simple validation check).
I've created the view model and have also written some code, but I'm not sure about my design and I also need help to complete the code. I've done some search, but I couldn't find a simple way of doing this. Please help. Thanks
View Model - Country.cs
public class Country
{ [Required]
public int CountryId { get; set; }
public IEnumerable<SelectListItem> Countries { get; set; }
}
Controller
public ActionResult CountriesDropDown()
{
using(ctx)
{
var dropdown = (from q in ctx.Countries
select new Models.Country
{
Id = q.Id,
//Need Code to populate the countries properties with country names from the database.
}
return view();
}
[HttpPost]
public ActionResult AddCountry(Models.Country country)
{
using(ctx)
{
}
return view();
}
Countries View
@model Models.Country
@Html.DropDownListFor(model => model.Country..?..Need code here as well, "please select")
@Html.ValidationFor(...Need code here to make sure the user selects a country)
You can also do it by alternate way,follow this one
public class Country { [Required] public int CountryId { get; set; } public string CountryName {get;set:} }
Controller
public ActionResult CountriesDropDown() { using(ctx) { var dropdown = (from q in ctx.Countries select new Models.Country { Id = q.Id, Name = q,CountryName //Need Code to populate the countries properties with country names from the database. } Viewbag.Country = dropdown; return view(); }
Countries View
@model Models.Country @Html.DropDownListFor(model => model.CountryId,new SelectListItem{@Viewbag.Country,CountryName,CountryId}, "please select");
这篇关于需要帮助来完成以下代码,以便在MVC中使用SelectListItem进行简单的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!