我尝试根据模型中的字段自动关注表单的不同区域。以下内容始终呈现自动对焦。
@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 })
有更好的方法吗?