<%= Html.EditorFor(product => product.Name) %>
我需要生成的输出设置 autocomplete="off" 属性。
我错过了什么?
编辑:我正在寻找 EditorFor 的扩展方法,它接受属性的键/值字典,所以我可以这样称呼它:<%= Html.EditorFor(product => product.Name, new { autocomplete = "off" } ) %>
这里是为LabelFor做的,但是需要针对EditorFor进行调整
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes) {
return LabelFor(html, expression, new RouteValueDictionary(htmlAttributes));
}
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
string labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
if (String.IsNullOrEmpty(labelText))
{
return MvcHtmlString.Empty;
}
TagBuilder tag = new TagBuilder("label");
tag.MergeAttributes(htmlAttributes);
tag.Attributes.Add("for", html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName));
tag.SetInnerText(labelText);
return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
}
Edit2:我意识到它不能被命名为 EditorFor,因为已经存在一个接受匿名类型的重写 EditorFor,请参见此处http://msdn.microsoft.com/en-us/library/ff406462.aspx .. 无论如何,我们可以换个名字,没什么大不了的。