Reputation: 1732
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"} );
Upvotes: 0
Views: 9186
Reputation: 12805
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);
}
Upvotes: 1
Reputation: 8503
You should probably define a method or a property in the model class that does the concatenation.
If that is not an option, try:
@Html.Label((Model.CatalogName + "-ProductNumber").ToString(), "Product Number")
Upvotes: 0
Reputation: 6725
@Html.Label(Model.CatalogName.ToString() + "-ProductNumber", "Product Number")
Upvotes: 0