在mvc razor中过滤DropDownList

时间:2013-12-11 10:25:09

标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor

我在mvc剃刀中有模特。在此模型(EcotourismAttractions)中,我有一个类ParameterList的列表。

我希望有三个DropDownList过滤项目EntityType

首先DropDownList显示EntityType,其值1DropDownList EntityType的值为2,第三个DropDownList显示{{ 1}},其值为EntityType

对于所有3 DropDownList参数和文本的标题是参数的ID。我可以这样做吗???

Value

1 个答案:

答案 0 :(得分:1)

我无法完全理解您的问题,如果可能的话,总是尽量让您的问题看起来很通用。无论如何,我能做的最好的就是......

为每个下拉列表创建三个IEnumerable。

public string Prop1 { get; set; }

public string Prop2 { get; set; }

public string Prop3 { get; set; }

public IEnumerable<SelectListItem> EntityType1List { get; set; }

public IEnumerable<SelectListItem> EntityType2List { get; set; }

public IEnumerable<SelectListItem> EntityType3List { get; set; }

使用所需的值初始化列表。

EntityType1List = new SelectList(ParametersList.Where(x=>x.EntityType == 1).ToList(), "Value","Text").ToList();

EntityType1List = new SelectList(ParametersList.Where(x=>x.EntityType == 2).ToList(), "Value","Text").ToList();

EntityType1List = new SelectList(ParametersList.Where(x=>x.EntityType == 3).ToList(), "Value","Text").ToList();

在视图上渲染

@Html.DropDownListFor(x => x.Prop1 , new SelectList(Model.EntityType1List , "Value", "Text", Model.Prop1))

@Html.DropDownListFor(x => x.Prop2, new SelectList(Model.EntityType2List , "Value", "Text", Model.Prop2))

@Html.DropDownListFor(x => x.Prop3, new SelectList(Model.EntityType3List , "Value", "Text", Model.Prop3))