如何从Html.EditorFor中呈现html属性

时间:2017-08-17 13:36:12

标签: asp.net-mvc razor

我尝试根据模型中的字段自动关注表单的不同区域。以下内容始终呈现自动对焦。

@Html.EditorFor(model => model.ManufacturerProduct.Specifications, new { htmlAttributes = new { @class = "form-control", autofocus = (Model.FocusOn == "Specifications" ? "" : null) } })

我设法做到这一点是为了让它发挥作用,但感觉很笨拙。

@{
    dynamic expando = new ExpandoObject();
    expando.@class = "form-control";
    if (Model.FocusOn == "Specifications") expando.autofocus = "autofocus";
}

@Html.EditorFor(model => model.ManufacturerProduct.Specifications, new { htmlAttributes = expando })

有更好的方法吗?

0 个答案:

没有答案