【问题标题】:Is it possible to combine Html.LabelFor CSS class and text value in MVC Razor?是否可以在 MVC Razor 中结合 Html.LabelFor CSS 类和文本值?
【发布时间】:2018-05-24 07:01:23
【问题描述】:

我一直在寻找一种将这种组合更改为更合适的解决方案的方法,因此输入(+隐藏)和标签是从 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 类”和“文本值”?

来自 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);

【问题讨论】:

  • 你可以创建一个自定义的HtmlHelper 扩展方法来呈现所有的html
  • 但是LabelFor() 方法有一个重载,接受文本作为第二个参数,htmlAttributes 作为第三个参数
  • DictionaryContent()返回什么? (我认为它不是string

标签: asp.net-mvc razor


【解决方案1】:

您可以将您的 css 类添加为 LabelFor() 函数的第三个参数,并确保您的 Sitecore().DictionaryContent 函数给出一个字符串值:

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

【讨论】:

  • 奇怪,Visual Studio 显示错误 => {{Argument 3: cannot convert from "" to 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" }))
猜你喜欢
  • 1970-01-01
  • 2015-09-11
  • 1970-01-01
  • 2018-03-24
  • 2016-04-29
  • 1970-01-01
  • 2012-06-24
  • 2013-03-04
  • 1970-01-01
相关资源
最近更新 更多