2013-12-11 60 views
-2

我有模型在mvc剃鬚刀。在這個模型(EcotourismAttractions)我有一個列表類ParameterList過濾器在mvc剃鬚刀DropDownList

我想要有三個DropDownList篩選項目EntityType

首先DropDownList顯示EntityType與值1DropDownListEntityType與值2和第三DropDownList顯示EntityType與值3

對於所有DropDownListValue是參數和文本的標題是參數的ID我可以這樣做嗎?

public class EcotourismAttractions 
    { 
     public int Id{ get; set; } 
     public string LatinName{ get; set; } 
     public List<AreaManagement.Entities.Parameters> ParametersList{ get; set; }  

    } 
public class Parameters { 
     public int Id { get; set; } 
     public byte EntityType { get; set; } 
     public int ParentId { get; set; } 
     public string Title { get; set; } 
    } 
+0

我們幾乎可以做的一切,但首先請提供一些格式,以您的問題,以便它會成爲易於閱讀。 – RollerCosta

+0

很明顯三個DropDownList基於EntityType值 –

+0

現在好了,編輯完成後。 – RollerCosta

回答

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)) 
+0

我不明白什麼是Prop1或Prop2或Prop3是? –

+0

Prop1,Prop2,Prop3是您在表單發佈時將保存選定下拉列表值的模型屬性。像任何其他模型屬性說公共字符串名字{get;設置;}和@ Html.TextBoxFor(x => x.FirstName) – RollerCosta

+0

最後ToList()初始化與期望值列表不正確 –