【问题标题】:MVC drop down list set selectedMVC 下拉列表集已选中
【发布时间】:2013-11-04 18:49:27
【问题描述】:

我正在尝试从模型值中设置下拉列表中选择的值。 有人可以帮助我设置模型值以被选中。

@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 表达式转换为类型“对象”,因为它不是委托类型

【问题讨论】:

  • 你能告诉我们你的模型传递给视图吗?

标签: asp.net-mvc asp.net-mvc-4


【解决方案1】:

如果您使用的是枚举,如评论中所述,请尝试我的枚举助手....

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() });
    }
}

【讨论】:

    【解决方案2】:

    您需要做的就是提供模型中的值和 SelectList 对象:

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

    其中Category 是模型上的属性,CategorySelectList 是模型上的SelectList 对象。

    我建议在您的模型中构建您的 SelectList 对象,这样您就不会因任何铸造或对象构建而使您的视图混乱。

    【讨论】:

    • 我试过这个仍然没有选择我在@Html.DropDownListFor(modelItem =&gt; item.Type, (SelectList)ViewBag.Category, "Select" )我的案例项目下面尝试过的值。类型是来自模型的枚举值。
    【解决方案3】:

    我通常在我的控制器中这样做:

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

    selectedValue 是你要选择的id。

    【讨论】:

      猜你喜欢
      • 2014-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多