@ Html.Label不允许字符串连接 [英] @Html.Label won't allow string concatenation

查看:80
本文介绍了@ Html.Label不允许字符串连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于局部视图构建动态表单.我的视图继承了一个具有单个属性的模型:CatalogName { get; set; }.

I'm building dynamic forms based on a partial view. My view inherits a model with a single property: CatalogName { get; set; }.

我已经尝试过了:

@Html.Label(Model.CatalogName + "-ProductNumber", "Product Number")
@Html.TextBox(Model.CatalogName + "-ProductNumber")

HTML呈现如下:

<label for>Product Number</label>
<input name="CatatalogName-ProductNumber" type="text" />

如果我这样编写代码:

@Html.Label("ProductNumber-" + Model.CatalogName", "Product Number")

它将以我期望的方式呈现

It will render the way I expect

<label for="ProductNumber-CatalogName">Product Number</label>

这是MVC的错误吗?关于为什么我的串联无法按标签中的要求进行操作,但可以与TextBox配合使用的问题,是否有任何答案?

Is this a bug with MVC? Are there any answers as to why my concatenation won't work the way I want it to in the label, but works fine with the TextBox?

我的模型如下:

public class Product
{
  public string CatalogName { get; set; }
}

我的局部视图继承了模型:

My partial view inherits the model:

@model Models.Product

我的控制器呈现如下操作结果:

My controller renders an action result like this:

return PartialView("_webForm", Models.Product { CatalogName = "CatalogName"} );

推荐答案

在寻找类似问题时,我偶然发现了这个线程.我找到了Microsoft(令人惊讶地)提供的源代码,并且至少找到了我的问题.

I happened to stumble across this thread while searching for a similar issue. I found the source code that Microsoft (surprisingly) provides, and found at least my issue.

因为参数注释不明确,所以我使用了错误的方法.我只是在寻找一种显示文本的简单方法.第一个参数应该是创建标签的相应控件的ID. SECOND参数是实际在标签显示部分显示的参数.

Because the comments for the parameters are not clear, I was using the method incorrectly. I was just looking for a simple way to display some text. The first parameter should be the ID of the corresponding control to which the Label is created. The SECOND parameter is what will actually display in the displaying section of the label.

因此,调用@Html.Label(parameter1),您将得到<label for="parameter1">parameter1</label>.

So with a call of @Html.Label(parameter1), you will get <label for="parameter1">parameter1</label>.

因为ID以."开头.在javascript和jQuery调用中,底层代码仅在最后一个."之后采用parameter1的最后一部分.

Because the ID begins with a "." in javascript and jQuery calls, the underlying code only takes the last section of parameter1 after the final ".".

我刚做完的事情就是创建自己的@ Html.Span扩展方法,以实现所需的功能.我了解这并不能完全回答MrGrigg的问题,但我希望其他发现此问题的人可以发现它有用.

What I just wound up doing was creating my own @Html.Span extension method to allow me to accomplish what I was looking for. I understand this doesn't exactly answer MrGrigg's question, but I hope someone else who finds this could find it useful.

这是所有@ Html.Label方法调用的代码:

Here is the code that all of the @Html.Label methods call:

internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string labelText = null) {
        string resolvedLabelText = labelText ?? metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
        if (String.IsNullOrEmpty(resolvedLabelText)) {
            return MvcHtmlString.Empty;
        }

        TagBuilder tag = new TagBuilder("label");
        tag.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)));
        tag.SetInnerText(resolvedLabelText);
        return tag.ToMvcHtmlString(TagRenderMode.Normal);
    }

这篇关于@ Html.Label不允许字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆