2011-11-24 43 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