2014-02-27 56 views
9

我加入以下行我的看法htmlAttributes MVC 5.1編輯器

@Html.EditorFor(model => model, new { htmlAttributes = new { @class = "form-control" }, }) 

如何訪問htmlAttributes在相應EditorTemplate Razor視圖?

另外我如何在EditorTemplate視圖的htmlAttributes中添加一些值到現有的傳遞?

+0

如果你真的想這樣做 - 我認爲你需要尋找到合格「htmlAttributes」作爲ExpandoObject而不是匿名類型。您可以使用擴展方法或類似的東西將您的對象轉換爲ExpandoObject。 –

+0

@LeeGunn - 你顯然不知道MVC 5.1已經在EditorFor Helper中添加了傳遞HtmlAttributes,所以現在支持 –

+0

@ErikTheViking - 是的,我不知道。謝謝。 –

回答

15

您可以從ViewData讓他們:

@{ 
    var htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(ViewData["htmlAttributes"]); 
    htmlAttributes.Add("key", "value"); //add new value 
} 
+0

@model字符串 @ { var htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(ViewData [「htmlAttributes」]); htmlAttributes.Add(「class」,「form-control」); //添加新值 } @ Html.TextBox(「」,ViewData.TemplateInfo.FormattedModelValue,htmlAttributes) –

+0

我們必須使用「htmlAttributes [」class「] + =」k-textbox fc-url「;」而不是「htmlAttributes.Add(」class「,」k-textbox fc-url「);」 –