2014-02-27 2 views
9

에 대한 내보기htmlAttributes MVC 5.1 편집기

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

가 어떻게 대응 EditorTemplate 면도기보기에서 htmlAttributes에 액세스 할 수있는 다음 줄을 추가 한?

또한 EditorTemplate보기에서 htmlAttributes에 전달 된 기존 값에 일부 값을 어떻게 추가합니까?

+0

당신이 정말로 그렇게하고 싶다면 - 당신이 익명의 유형보다는 ExpandoObject로 통과에 "htmlAttributes"를 볼 필요가 거라고 생각합니다. 확장 메서드 나 비슷한 것을 사용하여 개체를 ExpandoObject로 변환 할 수 있습니다. –

+0

@LeeGunn - MVC 5.1이 EditorFor 헬퍼에 전달 된 HtmlAttributes를 추가했음을 알지 못해서 이제는 지원됩니다. –

+0

@ErikTheViking - 네, 그 사실을 몰랐습니다. 감사. –

답변

15

당신은 그 (것)은 ViewData에서 얻을 수 있습니다

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

@ 모델 문자열 @ 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 ");" –

관련 문제