困惑于Html.AttributesFor(m => m.Name))
问题描述:
我正在尝试从其他人的代码中学习。我看到以下内容:困惑于Html.AttributesFor(m => m.Name))
@Html.TextBoxFor(m => m.Name, Html.AttributesFor(m => m.Name))
有人可以向我解释Html.AttributesFor的工作原理吗?这些属性是什么类型,我可以在哪里设置它们。
更新:
我发现下面隐藏在代码:
public static IDictionary<string, object> AttributesFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
{
var attributes = new RouteValueDictionary {{"class", ""}};
WhenEncountering<StringLengthAttribute>(expression, att => attributes["maxlength"] = att.MaximumLength);
WhenEncountering<HintSizeAttribute>(expression, att =>
{
attributes["class"] += att.Size.ToString().ToLowerInvariant() + " ";
});
attributes["class"] = attributes["class"].ToString().Trim();
return attributes;
}
答
TextBoxFor
助手有三个重载,没有人指定为您发布的语法,可能是其自定义的助手有人有为方便起见而写。的Html.TextBoxFor
第二个参数的HTML objectHtmlAttributes
,你可以指定一个像
@Html.TextBoxFor(x=>x.name,new { @class="classname", @rel="nofollow" })
或者它需要一个IDictionat<string,object>htmlAttributes
@Mohamed Meligy日Thnx为你在哪里解释什么'AttributesFor'做编辑 – Rafay
? – jgauffin
@jgauffin它是一个自定义的助手我怎么能确定它显然它指定的编辑器的HTML属性 – Rafay