Можно ли объединить Html.LabelFor CSS класс и текстовое значение в MVC Razor? - PullRequest
0 голосов
/ 24 мая 2018

Я искал способ изменить эту комбинацию на более подходящее решение, поэтому ввод (+ скрытый) и метка генерируются из бритвы.

@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))

Возможно ли сочетание "css class" и "text value"в Razor MVC?

Из 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 голосов
/ 24 мая 2018

Вы можете добавить свой класс css в качестве 3-го параметра для функции LabelFor() и быть уверенным, что ваша функция Sitecore().DictionaryContent дает строковое значение:

@Html.LabelFor(s => s.RememberMe, Html.Sitecore().DictionaryContent(Dictionary.LoginRememberMeFieldName, true).ToString(), new { @class = "yourCSSClass" }))
...