I have a couple of properties in my view model that are display-only but I need to retrieve their values using jQuery to perform a calculation on the page. The standard Html.DisplayFor() method just writes their value to the page. I want to create a razor template that will allow me to render each element as:
<span id="ElementsId">Element's value</span>
I know I can specify a template in Html.DisplayFor() to use a particular template for rendering the property but within that template how do I identify the id attribute to write into the span tag?
@Html.DisplayFor(model => model.Element, "MyTemplate");
You could make this
id
part of the view model and use it in the display template:I had exactly the same issue as the original post.
Not sure the last comment is valid. It would make the HTML id attribute a run-time value and therefore cannot be referenced with a design time name.
I used the overload of DisplayFor which allows you to add new objects onto the data dictionary (ViewBag)
My model is a C# object called Project with various properties. In my view I have this:
This is using a custom template called StringDisplaySetHtmlID and the last parameter adds a key value pair to the Viewbag.
My template file looks like this:
I'm also setting a class here for styling purposes. I've used the key name HtmlID rather than just ID to avoid a potential common naming collision.
Now in my javascript I can pick up the span's content using the following jquery:
There's an article explaining the
Templates
(Display + Editor) in Razor, and also theUIHint
attribute.I read many SO posts about defining template for
@Html.DisplayFor
for Boolean property but I couldn't understand them clearly. Your question is close to this and after grasping it, I decided to add a new answer includes completely all steps for implementing that. It might be helpful for other people.1. Creating a template
At first, you need to add a
Partial View
in path below (the path is very important):For example, I created a
Partial View
that named_ElementTemplate
and Fill it like this:2. Adding UIHint to the Model
For make a connection between your
property
andtemplate
, you should addUIHint
attribute like below in your model class:3. Using @Html.DisplayNameFor in Veiw
In every view that you need this property, you can use code below:
Output
The code above is rendered to code below in my example (
if (MyProperty == true)
):Setting attributes
For setting
id
or other html attributes you can use ModelMetadata like this:Output with attribute
OK, I found it and it's actually very simple. In my Views\Shared\DisplayTemplates folder I have Reading.cshtml containing the following:
This renders the correct tag using the name of the property as the id attribute and the value of the property as the contents:
In the view file this can be called using the following:
Or if the model property is decorated with UIHint("Reading") then the template name can be left out of the call to DisplayFor() and it will still render using the template:
This should work equally well with custom editor templates.