2011-11-24 98 views
0

我正在尝试从其他人的代码中学习。我看到以下内容:困惑于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; 
     } 

回答

1

TextBoxFor助手有三个重载,没有人指定为您发布的语法,可能是其自定义的助手有人有为方便起见而写。的Html.TextBoxFor第二个参数的HTML objectHtmlAttributes,你可以指定一个像

@Html.TextBoxFor(x=>x.name,new { @class="classname", @rel="nofollow" }) 

或者它需要一个IDictionat<string,object>htmlAttributes

+0

@Mohamed Meligy日Thnx为你在哪里解释什么'AttributesFor'做编辑 – Rafay

+0

? – jgauffin

+1

@jgauffin它是一个自定义的助手我怎么能确定它显然它指定的编辑器的HTML属性 – Rafay