Tim Vermaelen
Tim Vermaelen

Reputation: 7059

Is it possible to combine Html.LabelFor CSS class and text value in MVC Razor?

I was looking for a way to change this combination to a more suited solution, so the input (+hidden) and label are generated from 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>

This doesn't allow me to change the text value:

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

And this doesn't allow me to change the CSS class:

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

Is there a combination of "css class" and "text value" possible in Razor MVC?

From the 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);

Upvotes: 0

Views: 1111

Answers (1)

kkakkurt
kkakkurt

Reputation: 2800

You can add your css class as the 3rd parameter for LabelFor() function and be sure that your Sitecore().DictionaryContent function is giving a string value:

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

Upvotes: 2

Related Questions