Reputation: 32808
I am trying to learn from the code of others. I saw the following:
@Html.TextBoxFor(m => m.Name, Html.AttributesFor(m => m.Name))
Can someone explain to me how the Html.AttributesFor works? What kind of attributes are these and where can I set them up.
Update:
I found the following hidden in the code:
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;
}
Upvotes: 0
Views: 81
Reputation: 31043
TextBoxFor
helper has three overloads and no one specifies the syntax as you have posted, may be its a custom helper somebody has written for convenience. The second argument of Html.TextBoxFor
takes html objectHtmlAttributes
which you can specify like
@Html.TextBoxFor(x=>x.name,new { @class="classname", @rel="nofollow" })
or it takes a IDictionat<string,object>htmlAttributes
Upvotes: 1