如何为强类型模型填充 2 DropDownListFor
How to populate 2 DropDownListFor for Strongly Typed model
我需要做的是-->在具有2个不同模型的视图上绑定2个DropDownListFor,我知道这是不可能实现的,因为只有一个模型可以在单一视图上调用。
初始要求
@Html.DropDownListFor(M => M.MobileList, new SelectList(Model.MobileList,"Value", "Text"))
@Html.DropDownListFor(M => M.CountryList, new SelectList(Model.CountryList,"Value", "Text"))
我做了什么
使用绑定其中一个下拉列表
@Html.DropDownListFor(M => M.MobileList, new SelectList(Model.MobileList,"Value", "Text"))
其他用过的ViewBag.
@Html.DropDownList("Countrylist", (IEnumerable<SelectListItem>)ViewBag.Countrylist, new { id = "CountryId", @class = "form-control", @multiple = "multiple" })
但我需要使用@Html.DropDownListFor razor 代码创建这两个下拉菜单。
示例:
public class Model1{
public int MobileId {get;set;}
public Model2 Model2{get;set;}
public List<SelectListItem> MobileList {get;set;}
}
public class Model2{
public int CountryId {get;set;}
public List<SelectListItem> CountryList {get;set;}
}
正在查看
@model Model1
@Html.DropDownListFor(M => M.MobileId,Model.MobileList, new { @class ="form-control" }))
@Html.DropDownListFor(M => M.Model2.CountryId,Model.Model2.CountryList, new { @class ="form-control" }))
您也可以使用
@Html.DropDownListFor(M => M.Model2.CountryId, new SelectList((IEnumerable)ViewBag.Countrylist,"Value", "Text"), new { id = "CountryId", @class = "form-control", @multiple = "multiple" })
您可以如下创建父模型并在您的视图中使用它:
public class MainModel{
public int CountryId {get;set;}
public int MobileValue {get;set;}
public List<MobileList> {get;set;}
public List<CountryList> {get;set;}
}
然后在您看来:
@model MainModel
@Html.DropDownListFor(M => M.MobileValue, new SelectList(Model.MobileList,"Value", "Text"))
@Html.DropDownListFor(M => M.CountryId, new SelectList(Model.CountryList,"Value", "Text"))
public class MainModel{
public int CountryId {get;set;}
public int MobileValue {get;set;}
public Tuple<List<MobileList>,List<CountryList>> {get;set;}
}
Please use the tuple for multiple list if you have only one model.
我需要做的是-->在具有2个不同模型的视图上绑定2个DropDownListFor,我知道这是不可能实现的,因为只有一个模型可以在单一视图上调用。
初始要求
@Html.DropDownListFor(M => M.MobileList, new SelectList(Model.MobileList,"Value", "Text"))
@Html.DropDownListFor(M => M.CountryList, new SelectList(Model.CountryList,"Value", "Text"))
我做了什么
使用绑定其中一个下拉列表
@Html.DropDownListFor(M => M.MobileList, new SelectList(Model.MobileList,"Value", "Text"))
其他用过的ViewBag.
@Html.DropDownList("Countrylist", (IEnumerable<SelectListItem>)ViewBag.Countrylist, new { id = "CountryId", @class = "form-control", @multiple = "multiple" })
但我需要使用@Html.DropDownListFor razor 代码创建这两个下拉菜单。
示例:
public class Model1{
public int MobileId {get;set;}
public Model2 Model2{get;set;}
public List<SelectListItem> MobileList {get;set;}
}
public class Model2{
public int CountryId {get;set;}
public List<SelectListItem> CountryList {get;set;}
}
正在查看
@model Model1
@Html.DropDownListFor(M => M.MobileId,Model.MobileList, new { @class ="form-control" }))
@Html.DropDownListFor(M => M.Model2.CountryId,Model.Model2.CountryList, new { @class ="form-control" }))
您也可以使用
@Html.DropDownListFor(M => M.Model2.CountryId, new SelectList((IEnumerable)ViewBag.Countrylist,"Value", "Text"), new { id = "CountryId", @class = "form-control", @multiple = "multiple" })
您可以如下创建父模型并在您的视图中使用它:
public class MainModel{
public int CountryId {get;set;}
public int MobileValue {get;set;}
public List<MobileList> {get;set;}
public List<CountryList> {get;set;}
}
然后在您看来:
@model MainModel
@Html.DropDownListFor(M => M.MobileValue, new SelectList(Model.MobileList,"Value", "Text"))
@Html.DropDownListFor(M => M.CountryId, new SelectList(Model.CountryList,"Value", "Text"))
public class MainModel{
public int CountryId {get;set;}
public int MobileValue {get;set;}
public Tuple<List<MobileList>,List<CountryList>> {get;set;}
}
Please use the tuple for multiple list if you have only one model.