我想請用這個語法我自己的HTML輔助方法:什麼是MVC3 HTML輔助方法,這種數據類型?
@Html.BootstrapLabelFor(m => Model.Email) //Email is a simple string property.
這是我到目前爲止有:
public static MvcHtmlString BootstrapLabelFor<TModel, TValue>
(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TValue>> expression)
{
string html = String.Format("<label for='{0}' class='some-class' />",
expression.Email.DisplayName???? - NEED HELP HERE.);
return new MvcHtmlString(html);
}
所以這裏是它的要點。
我需要知道的是如何獲取DisplayName屬性(如果它被稱爲是)從表達式對象之內。假設這是我需要看的地方。
這裏是我LogOnModel類:
public class LogOnModel
{
[Required(ErrorMessage = "You must enter your email address.")]
[Display(Name = "Email:")]
public string Email { get; set; }
[Required(ErrorMessage = "You must enter your password.")]
[DataType(DataType.Password)]
[Display(Name = "Password:")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
現在,我們取得了一些進展。這是我現在擁有的。它的輸出與我想要的正確的屬性,但它不使用顯示名稱元裝飾我在模型中使用。只是吐出屬性名稱。有什麼建議麼?
public static MvcHtmlString BootstrapLabelFor<TModel, TValue>
(this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TValue>> expression)
{
string html = String.Format(
"<label for='{0}' class='control-label'>{0}</label>",
ExpressionHelper.GetExpressionText(expression));
return new MvcHtmlString(html);
}
我很困惑....應該是'Expresion>'其中'TModel'是你的模型和'TValue'通過投影暗示......正是它說.. 。? –
2012-03-13 20:15:19