C# htmlAttributes MVC 5.1编辑器
我在我的视图中添加了以下行C# htmlAttributes MVC 5.1编辑器,c#,asp.net-mvc,model-binding,C#,Asp.net Mvc,Model Binding,我在我的视图中添加了以下行 @Html.EditorFor(model => model, new { htmlAttributes = new { @class = "form-control" }, }) 如何在相应的EditorTemplateRazor视图中访问htmlAttributes 另外,如何在EditorTemplate视图中向现有传入的htmlAttributes添加一些值?您可以从视图数据中获取它们: @{ var htmlAttributes = Html
@Html.EditorFor(model => model, new { htmlAttributes = new { @class = "form-control" }, })
如何在相应的EditorTemplateRazor视图中访问htmlAttributes
另外,如何在EditorTemplate视图中向现有传入的htmlAttributes添加一些值?您可以从
视图数据中获取它们:
@{
var htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(ViewData["htmlAttributes"]);
htmlAttributes.Add("key", "value"); //add new value
}
如果您真的想这样做,我认为您需要考虑将“HtmlatAttributes”作为ExpandooObject而不是匿名类型传递。您可以使用扩展方法或类似的方法将您的对象转换为ExpandooObject。@LeeGunn-您显然不知道MVC 5.1在EditorFor helpers中添加了传递HtmlAttributes,所以现在是这样supported@ErikTheViking-是的,我不知道。谢谢。@model String@{var htmlAttributes=HtmlHelper.AnonymousObjectToHmlAttributes(ViewData[“htmlAttributes”]);htmlAttributes.Add(“class”,“form control”);//Add new value}@Html.TextBox(“”,ViewData.TemplateInfo.FormattedModelValue,htmlAttributes)我们必须使用“htmlAttributes[“class”+=“k-TextBox fc url”;”来代替“htmlAttributes.Add”(“class”,“k-textbox fc url”);"