问题
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 ?
回答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 ;]
来源:https://stackoverflow.com/questions/20357770/how-to-make-display-template-in-mvc-4-project