Reputation: 1491
I am new to MVC4 and don't understand why @Html.Label is chopping my string data as follows.
The string returned is "11, 12.2, 15.2, 17.1R, 18.3R, 21R", and @Html.Label is chopping everything before the last . character.
View
<td>@foo.GetString</td>
<td>@Html.Label(foo.GetString)</td>
Model
public string GetString { get
{
return "11, 12.2, 15.2, 17.1R, 18.3R, 21R";
}
}
Resulting markup
<tr>
<td>11, 12.2, 15.2, 17.1R, 18.3R, 21R</td>
<td>
<label for="">3R, 21R</label>
</td>
</tr>
I am using @foo.GetString as this displays the whole string but would like to understand why this happens please.
Upvotes: 0
Views: 1181
Reputation: 1
You can use second overload method of @Html.Label as below:
@Html.Label("", foo.GetString)
which will give you expected result.
Upvotes: 0
Reputation: 117
My solution is:
<label id=@("label_" + Model.Name) for=@("textBox_" + Model.Name)>@(Model.Caption)</label>
Upvotes: 0
Reputation: 19156
This is the LabelHelper method of LabelExtensions
class. source
internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata,
string htmlFieldName, string labelText = null,
IDictionary<string, object> htmlAttributes = 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);
tag.MergeAttributes(htmlAttributes, replaceExisting: true);
return tag.ToMvcHtmlString(TagRenderMode.Normal);
}
As you can see, it finds the last dot in the field's name htmlFieldName.Split('.').Last()
to make the resolvedLabelText
in your case. So you should not use it for displaying the raw data. Its main usage is displaying metadata.DisplayName
or metadata.PropertyName
.
You can try @Html.Raw
to show the content as it is (without any kinds of encoding).
Upvotes: 3