Asp.net mvc 如何为强类型模型填充2个DropDownList

Asp.net mvc 如何为强类型模型填充2个DropDownList,asp.net-mvc,razor,dropdownlistfor,Asp.net Mvc,Razor,Dropdownlistfor,我需要做的是-->在具有2个不同模型的视图上绑定2个DropDownList for,我知道这是无法实现的,因为在单个视图上只能调用一个模型 初始要求 @DropDownListFor(M=>M.MobileList,新选择列表(Model.MobileList,“Value”,“Text”)) @DropDownListFor(M=>M.CountryList,新选择列表(Model.CountryList,“Value”,“Text”)) 我所做的 使用 @DropDownListFor(M

我需要做的是-->在具有2个不同模型的视图上绑定2个DropDownList for,我知道这是无法实现的,因为在单个视图上只能调用一个模型

初始要求

@DropDownListFor(M=>M.MobileList,新选择列表(Model.MobileList,“Value”,“Text”))

@DropDownListFor(M=>M.CountryList,新选择列表(Model.CountryList,“Value”,“Text”))

我所做的

使用

@DropDownListFor(M=>M.MobileList,新选择列表(Model.MobileList,“Value”,“Text”))

用于其他使用过的可视包。

@Html.DropDownList("Countrylist", (IEnumerable<SelectListItem>)ViewBag.Countrylist, new { id = "CountryId", @class = "form-control", @multiple = "multiple" })
@Html.DropDownList(“Countrylist”,(IEnumerable)ViewBag.Countrylist,新的{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;}
}
你也可以使用

@Html.DropDownListFor(M =>  M.Model2.CountryId, new SelectList((IEnumerable)ViewBag.Countrylist,"Value", "Text"), new { id = "CountryId", @class = "form-control", @multiple = "multiple" })
例如:

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;}
}
你也可以使用

@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 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"))
公共类main模型{
public int CountryId{get;set;}
公共int-MobileValue{get;set;}
公共元组{get;set;}
}
如果只有一个模型,请对多个列表使用元组

公共类main模型{
public int CountryId{get;set;}
公共int-MobileValue{get;set;}
公共元组{get;set;}
}
如果只有一个模型,请对多个列表使用元组



你是否考虑使用这样的元组“元组”作为视图的模型?你是否考虑使用这样的元组“元组”作为视图的模型?我有2个单独的模型。M=>M.CountryList在MobileList型号上不可用您所说的两个独立型号是什么意思
MobileList
CountryList
存在于不同的型号中?是的,这是两个独立的型号,我不明白你想说什么。@RohitKumarI需要使用ViewBag数据将其绑定到我的下拉列表,并根据你的建议实现它。我只需要在第一个模型中调用我的第二个模型,它就工作了。我有两个独立的模型。M=>M.CountryList在MobileList型号上不可用您所说的两个独立型号是什么意思
MobileList
CountryList
存在于不同的型号中?是的,这是两个独立的型号,我不明白你想说什么。@RohitKumarI需要使用ViewBag数据将其绑定到我的下拉列表中,并根据你的建议实现它。我只需要在第一个模型中调用我的第二个模型,它就工作了。只想检查我们是否不能使用ViewBag,而使用@Html.DropDownListFor作为它的第一个参数是lamda表达式。@RohitKumar不能,因为它需要模型属性作为第一个参数。有什么方法可以扩展此方法吗?并避免使用MainModel?在以这种方式实现DropDownList之后,您实际上想要实现什么?可能还有其他方法,只是想检查在使用@Html.DropDownListFor作为第一个参数时是否不能使用ViewBag,因为它是一个lamda表达式。@RohitKumar不能,因为它需要模型属性作为第一个参数。我们有什么方法可以扩展此方法吗?并避免使用MainModel?在以这种方式实现DropDownList之后,您实际上想要实现什么?也许还有别的办法