2013-10-25 70 views
0

我想設置從我的下拉列表中選擇的值從模型值。 有人可以幫助我在這裏設置模型的值來選擇。MVC下拉列表集合選擇

@foreach (var item in Model) 
{ 
<p> 
    @Html.DisplayFor(modelItem => item.Type) 
</p> 
<p> 
@Html.DropDownList("categoryvalues", (IEnumerable<SelectListItem>)ViewBag.Category, "Select") 
</p> 
} 

嘗試以下

@Html.DropDownListFor(modelItem => item.Type, new SelectList((IEnumerable<SelectListItem>)ViewBag.Category,"Value","Text",modelItem => item.Type.ToString())) 

我收到錯誤

無法轉換lambda表達式鍵入「對象」,因爲它不是一個委託類型

+0

你能告訴我們你的模型傳遞給視圖? – Kaf

回答

0

如果您使用的是枚舉,因爲在您的評論中提到的,盡我的枚舉幫手....

http://jnye.co/Posts/4/creating-a-dropdown-list-from-an-enum-in-mvc-and-c%23

這將允許你做到以下幾點:

在你控制器:

//If you don't have an enum value use the type 
ViewBag.DropDownList = EnumHelper.SelectListFor<MyEnum>(); 

//If you do have an enum value use the value (the value will be marked as selected)  
ViewBag.DropDownList = EnumHelper.SelectListFor(MyEnum.MyEnumValue); 

在你看來:

@Html.DropDownList("DropDownList") 
@* OR *@ 
@Html.DropDownListFor(m => m.Property, ViewBag.DropDownList as SelectList, null) 

這裏是助手:

public static class EnumHelper 
{ 
    // Get the value of the description attribute if the 
    // enum has one, otherwise use the value. 
    public static string GetDescription<TEnum>(this TEnum value) 
    { 
     var fi = value.GetType().GetField(value.ToString()); 

     if (fi != null) 
     { 
      var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false); 

      if (attributes.Length > 0) 
      { 
       return attributes[0].Description; 
      } 
     } 

     return value.ToString(); 
    } 

    /// <summary> 
    /// Build a select list for an enum 
    /// </summary> 
    public static SelectList SelectListFor<T>() where T : struct 
    { 
     Type t = typeof(T); 
     return !t.IsEnum ? null 
         : new SelectList(BuildSelectListItems(t), "Value", "Text"); 
    } 

    /// <summary> 
    /// Build a select list for an enum with a particular value selected 
    /// </summary> 
    public static SelectList SelectListFor<T>(T selected) where T : struct 
    { 
     Type t = typeof(T); 
     return !t.IsEnum ? null 
         : new SelectList(BuildSelectListItems(t), "Text", "Value", selected.ToString()); 
    } 

    private static IEnumerable<SelectListItem> BuildSelectListItems(Type t) 
    { 
     return Enum.GetValues(t) 
        .Cast<Enum>() 
        .Select(e => new SelectListItem { Value = e.ToString(), Text = e.GetDescription() }); 
    } 
} 
0

所有你需要做的是提供您的模型和一個SelectList對象的值:

@Html.DropDownListFor(m => m.Category, Model.CategorySelectList, "Select a category") 

其中Category是您的模型的屬性,CategorySelectList是您的模型上的SelectList對象。

我建議在你的模型中建立你的SelectList對象,這樣你就不會在任何鑄件或對象建築物中混亂你的視野。

+0

我試過這個仍然沒有被選中的值我在下面試過'@ Html.DropDownListFor(modelItem => item.Type,(SelectList)ViewBag.Category,「Select」)'my case item.Type是模型中的一個枚舉值。 – user2107843

0

我通常做我的控制器內:

ViewBag.Category = new SelectList(SelectAllList(),"ID", "NAME",selectedValue); 

了selectedValue是你想要選擇的ID。