是否可以在MVC Razor中组合Html.LabelFor CSS类和文本值?

时间:2018-05-24 07:01:24

标签: asp.net-mvc razor

我正在寻找一种方法将这种组合更改为更合适的解决方案,因此输入(+隐藏)和标签是从剃刀生成的。

@Html.CheckBoxFor(s => s.RememberMe, new { @class = "custom-control-input" })
<label class="custom-control-label" for="flyoutRememberMe">
    @Html.Sitecore().DictionaryContent(Dictionary.LoginRememberMeFieldName, true)
</label>

这不允许我更改文本值:

@Html.CheckBoxFor(s => s.RememberMe, new { @class = "custom-control-input" })
@Html.LabelFor(s => s.RememberMe, new { @class = "custom-control-label" })

这不允许我更改CSS类:

@Html.LabelFor(s => s.RememberMe, Html.Sitecore().DictionaryContent(Dictionary.LoginRememberMeFieldName, true))

Razor MVC中是否可以组合使用“css class”和“text value”?

来自LabelExtension.cs

/// <summary>Returns an HTML label element and the property name of the property that is represented by the specified expression using the label text.</summary>
/// <returns>An HTML label element and the property name of the property that is represented by the expression.</returns>
/// <param name="html">The HTML helper instance that this method extends.</param>
/// <param name="expression">An expression that identifies the property to display.</param>
/// <param name="labelText">The label text to display.</param>
/// <typeparam name="TModel">The type of the model.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string labelText);
/// <summary>Returns an HTML label element and the property name of the property that is represented by the specified expression.</summary>
/// <returns>An HTML label element and the property name of the property that is represented by the expression.</returns>
/// <param name="html">The HTML helper instance that this method extends.</param>
/// <param name="expression">An expression that identifies the property to display.</param>
/// <param name="htmlAttributes">An object that contains the HTML attributes to set for the element.</param>
/// <typeparam name="TModel">The type of the model.</typeparam>
/// <typeparam name="TValue">The value.</typeparam>
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes);
/// <summary>Returns an HTML label element and the property name of the property that is represented by the specified expression.</summary>
/// <returns>An HTML label element and the property name of the property that is represented by the expression.</returns>
/// <param name="html">The HTML helper instance that this method extends.</param>
/// <param name="expression">An expression that identifies the property to display.</param>
/// <param name="htmlAttributes">An object that contains the HTML attributes to set for the element.</param>
/// <typeparam name="TModel">The type of the model.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes);
/// <summary>Returns an HTML label element and the property name of the property that is represented by the specified expression.</summary>
/// <returns>An HTML label element and the property name of the property that is represented by the expression.</returns>
/// <param name="html">The HTML helper instance that this method extends.</param>
/// <param name="expression">An expression that identifies the property to display.</param>
/// <param name="labelText">The label text.</param>
/// <param name="htmlAttributes">An object that contains the HTML attributes to set for the element.</param>
/// <typeparam name="TModel">The type of the model.</typeparam>
/// <typeparam name="TValue">The Value.</typeparam>
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string labelText, object htmlAttributes);
/// <summary>Returns an HTML label element and the property name of the property that is represented by the specified expression.</summary>
/// <returns>An HTML label element and the property name of the property that is represented by the expression.</returns>
/// <param name="html">The HTML helper instance that this method extends.</param>
/// <param name="expression">An expression that identifies the property to display.</param>
/// <param name="labelText">The label text to display.</param>
/// <param name="htmlAttributes">An object that contains the HTML attributes to set for the element.</param>
/// <typeparam name="TModel">The type of the model.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string labelText, IDictionary<string, object> htmlAttributes);

1 个答案:

答案 0 :(得分:2)

您可以将css类添加为/login函数的第3个参数,并确保LabelFor()函数提供字符串值:

Sitecore().DictionaryContent