首页 文章

mvc3 .net 使用 Html.DisplayFor 显示视图模板

提问于
浏览
0

我正在尝试使用模板。

我在使用 Html.DisplayFor 扩展名时遇到了麻烦

我希望模板将所有数据显示为 read-only。我相信 HTML.DisplayFor 可以做到这一点吗?

如果是这样,我将 IEnumerable 模型传递给视图:

@model IEnumerable<Tens.Models.Applicant>

@{   
    if (Model != null)
    {  
          @Html.DisplayForModel("ApplicationOutcome") 
    } 
 }

和我的模板:

@model IEnumerable<Tens.Models.Applicant>

@{

foreach(var item in Model)
{
    @Html.TextBox("forenames",item.Forenames)
    
@Html.TextBox("surname",item.Surname)
<text>----------------------------------</text> <br> } }

上面的代码工作正常(显示 2 条记录),但这意味着我将每个字段 readonly 属性设置为 true(有更多字段加载 - 这么乏味)。我想要做的是使用 Html.DisplayFor 以只读方式显示这些数据,但我尝试了许多不同的方法,但无法使其工作。任何帮助表示赞赏。

3 回答

  • 1

    你的代码很奇怪。我就是这样做的。

    WhateverView.cshtml

    @model IEnumerable<Tens.Models.Applicant>  
    
    @Html.DisplayForModel()
    

    然后,在 DisplayTemplate 文件夹中:

    Applicant.cshtml

    @model Tens.Models.Applicant
    
    @Html.LabelFor(m => Forenames)
    @Html.DisplayFor(m => m.Forenames)
    
    @Html.LabelFor(m => Surnames)
    @Html.DisplayFor(m => m.Surnames)
    

    使用这样的模板的关键是模型是一个集合,模板只适用于单个项目(注意模板中没有 IEnumerable)。

  • 0

    我发现如果我在模板中执行以下操作,我会按预期获得数据:

    @model IEnumerable<Tens.Models.Applicant>
    
    @{
    
    foreach(var item in Model)
    {
        @Html.DisplayFor(m=>item.Forenames)
        
    @Html.DisplayFor(m=>item.Surname)
    <text>----------------------------------</text>
    } }
  • 0

    如果您想了解更多相关信息,请查看 Brad Wilson 的这篇文章。 http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html基本上您看到的问题是由于在类型对象的默认显示模板中进行了此检查

    ViewData.TemplateInfo.TemplateDepth > 1
    

    而不是打电话

    @Html.DisplayForModel("ApplicationOutcome")
    

    在您的视图中调用另一个模板。放

    foreach(var item in Model)
    {
        @Html.DisplayFor(m=>item)
    }
    

    在你的视图中直接。这应该可以得到预期的结果。

    下面是我的 MVC Controller/Model/View 文件。你能试试这些吗?我没有任何特定于模型的模板文件,这是关键。会发生什么是 mvc 将使用默认模板来渲染该对象。

    DefaultController.cs(控制器)

    public class DefaultController : Controller
    {
        //
        // GET: /Default/
    
        public ActionResult Index()
        {
            var model = new List<Applicant>();
            model.Add(new Applicant{FirstName = "foo",LastName = "bar", Address = "Foo Bar"});
            model.Add(new Applicant { FirstName = "foo1", LastName = "bar1", Address = "Foo1 Bar1" });
            model.Add(new Applicant { FirstName = "foo2", LastName = "bar2", Address = "Foo2 Bar2" });
    
            return View(model);
        }
    }
    

    Application.cs(型号)

    public class Applicant
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
    }
    

    Index.cshtml(查看)

    @model IEnumerable<MvcApplication1.Models.Applicant>
    @{
        Layout = null;
    }
    <html>
       <head>
         <title>this is a test</title>
       </head>
    <body>
        <h1>This is a test</h1>
        @if (Model != null)
        {
            foreach (var item in Model)
            {
                @Html.DisplayFor(m => item)
                
    } } </body>

    结果输出

    结果

相关问题