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

Asp.net mvc 是否可以在MVC Razor中组合CSS类的Html.LabelFor和文本值?,asp.net-mvc,razor,Asp.net Mvc,Razor,我正在寻找一种方法,将这种组合更改为更适合的解决方案,因此输入(+隐藏)和标签是从razor生成的 @Html.CheckBoxFor(s => s.RememberMe, new { @class = "custom-control-input" }) <label class="custom-control-label" for="flyoutRememberMe"> @Html.Sitecore().DictionaryContent(Dictionary.Log

我正在寻找一种方法,将这种组合更改为更适合的解决方案,因此输入(+隐藏)和标签是从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>
这不允许我更改CSS类:

@Html.LabelFor(s => s.RememberMe, Html.Sitecore().DictionaryContent(Dictionary.LoginRememberMeFieldName, true))
在Razor MVC中是否有“css类”和“文本值”的组合

来自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);
///返回HTML标签元素和使用标签文本由指定表达式表示的属性的属性名称。
///HTML标签元素和由表达式表示的属性的属性名称。
///此方法扩展的HTML帮助程序实例。
///标识要显示的属性的表达式。
///要显示的标签文本。
///模型的类型。
///值的类型。
公共静态MvcHtmlString LabelFor(此HtmlHelper html、表达式、字符串labelText);
///返回由指定表达式表示的属性的HTML标签元素和属性名称。
///HTML标签元素和由表达式表示的属性的属性名称。
///此方法扩展的HTML帮助程序实例。
///标识要显示的属性的表达式。
///包含要为元素设置的HTML属性的对象。
///模型的类型。
///价值。
公共静态MvcHtmlString LabelFor(此HtmlHelper html、表达式、对象htmlAttributes);
///返回由指定表达式表示的属性的HTML标签元素和属性名称。
///HTML标签元素和由表达式表示的属性的属性名称。
///此方法扩展的HTML帮助程序实例。
///标识要显示的属性的表达式。
///包含要为元素设置的HTML属性的对象。
///模型的类型。
///值的类型。
公共静态MvcHtmlString LabelFor(此HtmlHelper html、表达式、IDictionary htmlAttributes);
///返回由指定表达式表示的属性的HTML标签元素和属性名称。
///HTML标签元素和由表达式表示的属性的属性名称。
///此方法扩展的HTML帮助程序实例。
///标识要显示的属性的表达式。
///标签文本。
///包含要为元素设置的HTML属性的对象。
///模型的类型。
///价值。
公共静态MvcHtmlString LabelFor(此HtmlHelper html、表达式表达式、字符串labelText、对象htmlAttributes);
///返回由指定表达式表示的属性的HTML标签元素和属性名称。
///HTML标签元素和由表达式表示的属性的属性名称。
///此方法扩展的HTML帮助程序实例。
///标识要显示的属性的表达式。
///要显示的标签文本。
///包含要为元素设置的HTML属性的对象。
///模型的类型。
///值的类型。
公共静态MvcHtmlString LabelFor(此HtmlHelper html、表达式、字符串labelText、IDictionary htmlAttributes);

您可以将css类添加为
LabelFor()
函数的第三个参数,并确保
Sitecore()。DictionaryContent
函数提供了字符串值:

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

您可以将css类添加为
LabelFor()
函数的第三个参数,并确保
Sitecore()。DictionaryContent
函数提供了字符串值:

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

您可以创建一个自定义的
HtmlHelper
扩展方法来呈现所有的HTMLB,但是
LabelFor()
方法有一个重载,它接受文本作为第二个参数,接受
htmlAttributes
作为第三个参数,您
DictionaryContent()返回什么?(我假设它不是
string
)您可以创建一个定制的
HtmlHelper
扩展方法来呈现所有的HTML,但是
LabelFor()
方法有一个重载,它接受文本作为第二个参数,接受
htmlAttributes
作为第三个参数,您
DictionaryContent()
返回什么?(我假设它不是
字符串
)奇怪,VisualStudio显示了一个错误=>{{{参数3:无法从“”转换为bool}}}。我将尝试一下……正如我所说,错误按预期发生。也许有一个属性可以填充文本?=>
new{@class=“my css class”,value=“my dictionary”}
可能是您的“System.Web.WebPages.Razor”版本导致了此问题。我在Razor版本3.0.0.0中测试了它,没有显示错误。您的版本是什么?版本:3.0.30128.0,是否可能与Sitecore CMS相关?我将在问题中添加扩展选项…如果使用普通字符串输入进行尝试,结果如何?比如:@Html.LabelFor(s=>s.RememberMe,“testLabel”,new{@class=“yourCSSClass”})奇怪,VisualStudio显示了一个错误=>{{{参数3:无法从“”转换为bool}},我将尝试一下……正如我所说的,错误发生得和预期的一样。也许有一个属性可以填充文本?=>
new{@class=“my css class”,value=“my dictionary”}
可能是您的“System.Web.WebPages.Razor”版本导致了此问题。我在Razor版本3.0.0.0中测试了它,没有显示错误。您的版本是什么?版本:3.0.30128.0,是否可能与Sitecore CMS相关?我将在问题中添加扩展选项…如果使用普通字符串输入进行尝试,结果如何?比如:@Html.LabelFor(s=>s.RememberMe,“testLabel”,new{@class=“yourCSSClass”}))