Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
485 views
in Technique[技术] by (71.8m points)

asp.net mvc 3 - @Html.Label won't allow string concatenation

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

I have tried this:

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

The HTML renders like this though:

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

If I write my code like this:

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

It will render the way I expect

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

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?

My model looks like this:

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"} );
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.

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

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

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.

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);
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...