How to make display template in MVC 4 project

两盒软妹~` 提交于 2019-12-10 15:22:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!