如何在MVC 4项目中制作显示模板 [英] How to make display template in MVC 4 project

查看:67
本文介绍了如何在MVC 4项目中制作显示模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在MVC中构建应用程序,我想使用显示模板在视图中显示我的模型.这是我的模板,但是当我尝试显示它时却给了我错误:

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>*@

我希望此模板显示在td中填充数据库数据的每一行,但是它什么也没做.我在做什么错了?

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 ?

推荐答案

首先,尝试在视图中使用@Html.DisplayFor(yourModel, "YourTemplateName").

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

在执行默认模板时,应将它们与模型相关联,并将其放在共享"文件夹中,以便它将与它们相关联.在您的情况下,您应该进行与模型相关的DisplayFor:

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 ;]

这篇关于如何在MVC 4项目中制作显示模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆