Reputation: 403
I am trying to create an id for an input by doing the following
id="@ViewData.ModelMetadata.ContainerType.Name + "_" + @ViewData.TemplateInfo.GetFullHtmlFieldId("FromDate")"
but it is coming through with id="propertyName +". How can I concatenate the two together with an underscore in the middle in a razor view?
Upvotes: 2
Views: 1057
Reputation: 57718
You can use the @(expression)
syntax to express an explicit code expression.
You need to be explicit here because the space after @ViewData.ModelMetadata.ContainerType.Name
is not a valid character for a C# identifier so the evalutation stops there.
The algorithm used by Razor for processing implicit code expression is the following:
(Taken from ScottGu's Blog)
Upvotes: 4
Reputation: 20230
id="@(ViewData.ModelMetadata.ContainerType.Name + "_" + ViewData.TemplateInfo.GetFullHtmlFieldId("FromDate"))"
Upvotes: 2
Reputation: 1039408
Try like this:
id="@(ViewData.ModelMetadata.ContainerType.Name)_@(ViewData.TemplateInfo.GetFullHtmlFieldId("FromDate"))"
or like this:
id="@string.Format("{0}_{1}", ViewData.ModelMetadata.ContainerType.Name, ViewData.TemplateInfo.GetFullHtmlFieldId("FromDate"))"
Upvotes: 3