Html.EnumDropDownListFor根据枚举变量的值

时间:2016-03-04 16:02:03

标签: c# asp.net-mvc asp.net-mvc-5

这可能是重复的,所以我已经指出我在这个网站上的阅读能让我取得一些进展......

我的模型定义如下:

public enum RequestType
{
    [Display(Name = "Lovely Cold Beer")]
    Beer = 0,
    [Display(Name = "Warm Tea")]
    Tea = 1,
    [Display(Name = "Milky Coffee")]
    Coffee= 2
}

根据网址,我有一个变量,用于自动选择合适的列表项,例如

http://example.com/Request/Tea

将在控制器中执行此操作...

ViewBag.RequestType = RequestType.Tea.ToString();
return View("Index");

在我看来,我有一个变量来读回这个值,然后显示适当的内容:

if (ViewBag.RequestType != null)
{
    reqType = Enum.Parse(typeof(RequestType), ViewBag.RequestType);
}

在此视图中,我使用以下命令创建下拉列表:

@Html.EnumDropDownListFor(model => model.RequestType, htmlAttributes: new { @onchange = "YadaYada();" })

这会使用为每个Display Name定义的Enum值来呈现列表。我需要的是在呈现页面时自动选择适当的列表项,它与reqType的值匹配。

从我的研究中我发现我可以像这样传递变量:

@Html.EnumDropDownListFor(model => model.RequestType, reqType.ToString(), htmlAttributes: new { @onchange = "YadaYada();" })

但是这会创建一个包含枚举值而不是显示名称的新列表项,例如

Tea <-- This should not be created, instead 'Warm Tea' should be selected
Lovely Cold Beer
Warm Tea
Milky Coffee

我的整个方法可能是错误的,因为我是MVC的新手,但我欢迎建议来解决它!我不明白为什么在控制器中,在枚举值上使用ToString会在EnumDropDownListFor方法中创建不同的结果。

2 个答案:

答案 0 :(得分:1)

reqType.ToString()方法的第二个参数(EnumDropDownListFor())正在使用overload,其中添加了optionLabel(一个带有null值的选项验证)。它不会设置所选选项的值。

MVC的模型绑定功能通过绑定到您的属性来工作,并且由于RequestType的默认值为"Beer",因此将选择该选项。

例如,在将模型传递给视图之前,您需要在模型中设置属性的值(假设您有/Request/{request}的特定路径)

public ActionResult Request(RequestType request)
{
    var model = new MyModel
    {
        RequestType = request
    };
    return View(model);
}

答案 1 :(得分:0)

您必须使用html扩展名。例如,

 public static string GetInputName<TModel, TProperty>(Expression<Func<TModel, TProperty>> expression)
    {
        if (expression.Body.NodeType == ExpressionType.Call)
        {
            MethodCallExpression methodCallExpression = (MethodCallExpression)expression.Body;
            string name = GetInputName(methodCallExpression);
            return name.Substring(expression.Parameters[0].Name.Length + 1);

        }
        return expression.Body.ToString().Substring(expression.Parameters[0].Name.Length + 1);
    }

    private static string GetInputName(MethodCallExpression expression)
    {
        // p => p.Foo.Bar().Baz.ToString() => p.Foo OR throw...
        MethodCallExpression methodCallExpression = expression.Object as MethodCallExpression;
        if (methodCallExpression != null)
        {
            return GetInputName(methodCallExpression);
        }
        return expression.Object.ToString();
    }

    public static MvcHtmlString EnumDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes) where TModel : class
    {
        string inputName = GetInputName(expression);
        var value = htmlHelper.ViewData.Model == null
            ? default(TProperty)
            : expression.Compile()(htmlHelper.ViewData.Model);

        return htmlHelper.DropDownList(inputName, ToSelectList(typeof(TProperty), value.ToString()), htmlAttributes);
    }
相关问题