How to make display template in MVC 4 project
Asked Answered
G

1

4

Hi i'm building application in MVC and i want to use a display template to display my model in a view. This is my template, but it gives me error when I try to display it:

@*<tr>
<td>@Html.Display(NameE)</td>
<td>@Html.Display(NameC)</td>
<td>@Html.Display(NameR)</td>
<td>@Html.Display(Timespan) </td>
<td><button class="button" type="submit" name="Review"></button></td>
</tr>*@

I want this template to display each row filled with database data in the td's, but it is doing nothing. What am I doing wrong ?

Gardol answered 3/12, 2013 at 17:28 Comment(2)
Would you please add your model class and most part of your view?Winsor
I just commented my code cause it was displaying like 20 times the same output.Gardol
H
1

First, try using @Html.DisplayFor(yourModel, "YourTemplateName") in your view.

When you are doing default templates you should relate them to the model and put them in your Shared folder so it will relate to them. In your case you should do model related DisplayFor:

@model YourModel

<tr>
  <td>@Html.DisplayFor(x => x.NameE)</td>
  <td>@Html.DisplayFor(x => x.NameC)</td>
  <td>@Html.DisplayFor(x => x.NameR)</td>
  <td>@Html.DisplayFor(x => x.Timespan) </td>
  <td><button class="button" type="submit" name="Review"></button></td>
</tr>

Then in your controller you should just use it like this:

@Html.DisplayFor(Model)

Take a look at this article for more information. Hope this helps ;]

Hildagarde answered 3/12, 2013 at 17:36 Comment(3)
i have related everything to my model it is still not displaying what i need to ..Gardol
i did here was that i needed display template name in my display call :D thanksGardol
please edit your question it is not clear what your view model and your error is ;] i'm glad it helpedHildagarde

© 2022 - 2024 — McMap. All rights reserved.