@Html.Label()

 

@Html.Label() 有六種多載方法,只舉例需要說明的部份

public static MvcHtmlString Label(this HtmlHelper html, string expression);
public static MvcHtmlString Label(this HtmlHelper html, string expression, string labelText);
public static MvcHtmlString Label(this HtmlHelper html, string expression, object htmlAttributes);
public static MvcHtmlString Label(this HtmlHelper html, string expression, IDictionary<string, object> htmlAttributes);
public static MvcHtmlString Label(this HtmlHelper html, string expression, string labelText, object htmlAttributes);
public static MvcHtmlString Label(this HtmlHelper html, string expression, string labelText, IDictionary<string, object> htmlAttributes);

 

一、public static MvcHtmlString Label(this HtmlHelper html, string expression);

於 .cshtml 使用

@Html.Label("StudentName")

HTML 呈現為

 

二、public static MvcHtmlString Label(this HtmlHelper html, string expression, string labelText);

於 .cshtml 使用

@Html.Label("StudentName", "Tom")

HTML 呈現為

 

三、public static MvcHtmlString Label(this HtmlHelper html, string expression, object htmlAttributes);

於 .cshtml 使用

@Html.Label("StudentName", new { @class = "text-danger" })

HTML 呈現為

 

四、public static MvcHtmlString Label(this HtmlHelper html, string expression, IDictionary<string, object> htmlAttributes);

於 .cshtml 使用

@{
    Dictionary<string, object> myClass = new Dictionary<string, object>();
    myClass.Add("class", "text-danger");
}
@Html.Label("StudentName", myClass)

HTML 呈現為

 

參考資料:

LabelExtensions.Label Method

HtmlHelper - Label