Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# htmlAttributes MVC 5.1编辑器_C#_Asp.net Mvc_Model Binding - Fatal编程技术网

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”);"