如何在 DropDownListFor 中绑定 CountryNames
How can I bind CountryNames in DropDownListFor
我正在使用 mvc4。如何在 DropdownList 中绑定 CuntryName 及其值
国家/地区?
public class Country
{
public int Cnt_Id { get; set; }
public string Cnt_Name { get; set; }
}
这是我的私人class
public class HybridEmployee
{
public IEnumerable<Country> GetCount { get; set; }
}
控制器
public ActionResult GetCountry()
{
var x = ObjRepo.GetCountry();
hybrid.GetCount = x;
return View(hybrid);
}
Index.cshtml
@model Mvc_Application.Models.HybridEmployee
@using Mvc_Application.Models
@using (Html.BeginForm("SaveEmp", "Home", FormMethod.Post))
{
@Html.DropDownListFor(x=>x.GetCount.FirstOrDefault().Cnt_Id),new SelectList(Model.GetCount,"","");
}
我们可以有两种方法,如下所示:
- 使用包含下拉列表数据的
ViewBag
。
模型文件:
public class State
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public int CountryID { get; set; }
}
在 .cs 文件中:
ViewBag.Countries = countryService.All().Select(x => new SelectListItem() { Text = x.Name, Value = x.Id.ToString() }).ToList();
在 .cshtml 文件中:
@Html.DropDownListFor(x => x.CountryID, ViewBag.Countries as IEnumerable<SelectListItem>, "Select Country", new { @class = "form-control" })
- 使用包含下拉列表数据的模型 属性。
模型文件:
public class State
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public int CountryID { get; set; }
public List<SelectListItem> Countries { get; set; }
}
在 .cs 文件中:
model.Countries = countryService.All().Select(x => new SelectListItem() { Text = x.Name, Value = x.Id.ToString() }).ToList();
在 .cshtml 文件中:
@Html.DropDownListFor(x => x.CountryID, Model.Countries, "Select Country", new { @class = "form-control" })
这个表达式完全没有意义:
@Html.DropDownListFor(x=>x.GetCount.FirstOrDefault().Cnt_Id),new SelectList(Model.GetCount,"","");
DropDownListFor
帮助程序 (Expression<Func<TModel, TProperty>> expression
) 的第一个参数不使用 LINQ 表达式,它是模型绑定表达式 - 您必须使用 属性 来保存选定的值。下拉列表绑定应该这样使用:
型号
public class Country
{
public int Cnt_Id { get; set; }
public string Cnt_Name { get; set; }
public int SelectedCountry { get; set; } // additional property to get selected value
}
查看
@Html.DropDownListFor(x => x.SelectedCountry, new SelectList(Model.GetCount, "Cnt_Id", "Cnt_Value"), null)
我正在使用 mvc4。如何在 DropdownList 中绑定 CuntryName 及其值 国家/地区?
public class Country
{
public int Cnt_Id { get; set; }
public string Cnt_Name { get; set; }
}
这是我的私人class
public class HybridEmployee
{
public IEnumerable<Country> GetCount { get; set; }
}
控制器
public ActionResult GetCountry()
{
var x = ObjRepo.GetCountry();
hybrid.GetCount = x;
return View(hybrid);
}
Index.cshtml
@model Mvc_Application.Models.HybridEmployee
@using Mvc_Application.Models
@using (Html.BeginForm("SaveEmp", "Home", FormMethod.Post))
{
@Html.DropDownListFor(x=>x.GetCount.FirstOrDefault().Cnt_Id),new SelectList(Model.GetCount,"","");
}
我们可以有两种方法,如下所示:
- 使用包含下拉列表数据的
ViewBag
。
模型文件:
public class State
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public int CountryID { get; set; }
}
在 .cs 文件中:
ViewBag.Countries = countryService.All().Select(x => new SelectListItem() { Text = x.Name, Value = x.Id.ToString() }).ToList();
在 .cshtml 文件中:
@Html.DropDownListFor(x => x.CountryID, ViewBag.Countries as IEnumerable<SelectListItem>, "Select Country", new { @class = "form-control" })
- 使用包含下拉列表数据的模型 属性。
模型文件:
public class State
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public int CountryID { get; set; }
public List<SelectListItem> Countries { get; set; }
}
在 .cs 文件中:
model.Countries = countryService.All().Select(x => new SelectListItem() { Text = x.Name, Value = x.Id.ToString() }).ToList();
在 .cshtml 文件中:
@Html.DropDownListFor(x => x.CountryID, Model.Countries, "Select Country", new { @class = "form-control" })
这个表达式完全没有意义:
@Html.DropDownListFor(x=>x.GetCount.FirstOrDefault().Cnt_Id),new SelectList(Model.GetCount,"","");
DropDownListFor
帮助程序 (Expression<Func<TModel, TProperty>> expression
) 的第一个参数不使用 LINQ 表达式,它是模型绑定表达式 - 您必须使用 属性 来保存选定的值。下拉列表绑定应该这样使用:
型号
public class Country
{
public int Cnt_Id { get; set; }
public string Cnt_Name { get; set; }
public int SelectedCountry { get; set; } // additional property to get selected value
}
查看
@Html.DropDownListFor(x => x.SelectedCountry, new SelectList(Model.GetCount, "Cnt_Id", "Cnt_Value"), null)