简体   繁体   English

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

[英]mvc3 .net Display View Template using Html.DisplayFor

I'm trying to use templates. 我正在尝试使用模板。

I'm having trouble using using the Html.DisplayFor extension 使用Html.DisplayFor扩展名时遇到问题

I want the template to display all the data as read-only. 我希望模板将所有数据显示为只读。 I believe that HTML.DisplayFor can do this? 我相信HTML.DisplayFor可以做到这一点?

If this is so, I have a IEnumerable model being passed to the view: 如果是这样,我有一个IEnumerable模型传递给视图:

@model IEnumerable<Tens.Models.Applicant>

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

and my Template: 和我的模板:

@model IEnumerable<Tens.Models.Applicant>

@{

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

The above code works fine (2 records displayed) but it would mean me setting each fields readonly attribute to true (there are a load more fields - so tedious). 上面的代码可以正常工作(显示2条记录),但是这意味着我将每个字段的readonly属性设置为true(有更多的字段加载-如此乏味)。 What I want to do is use Html.DisplayFor to display this data as readonly, but I've tried numerous different ways and can't get it to work. 我想要做的是使用Html.DisplayFor将数据显示为只读,但是我尝试了许多不同的方法,但无法使其正常工作。 Any help is appreciated. 任何帮助表示赞赏。

Your code is quite strange. 您的代码很奇怪。 Here is how I would do it. 这就是我要怎么做。

WhateverView.cshtml WhateverView.cshtml

@model IEnumerable<Tens.Models.Applicant>  

@Html.DisplayForModel()

Then, in the DisplayTemplate folder: 然后,在DisplayTemplate文件夹中:

Applicant.cshtml Applicant.cshtml

@model Tens.Models.Applicant

@Html.LabelFor(m => Forenames)
@Html.DisplayFor(m => m.Forenames)

@Html.LabelFor(m => Surnames)
@Html.DisplayFor(m => m.Surnames)

etc.. The key to using a template like this where your model is a collection is that the template only works with a single item (note there is no IEnumerable in the template). 等。在您的模型是集合的情况下使用这样的模板的关键是,该模板仅适用于单个项目(请注意,模板中没有IEnumerable)。

You should take a look at this article by Brad Wilson if you want to know more on this. 如果您想了解更多有关此内容的信息,请阅读Brad Wilson的这篇文章。 http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html Basically the issue you are seeing is due to this check in the default display template for type object http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html基本上,您看到的问题是由于选中了默认显示模板类型对象

ViewData.TemplateInfo.TemplateDepth > 1

Rather than calling 而不是打电话

@Html.DisplayForModel("ApplicationOutcome") 

in your view which calls another template. 在您的视图中调用了另一个模板。 Put

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

in your view directly. 直接在您看来。 This should get you the expected result. 这应该为您带来预期的结果。

Below are my MVC Controller/Model/View files. 以下是我的MVC控制器/模型/视图文件。 Can you try with these. 你可以试试这些吗? I don't have any templates files specific to the model which is the key. 我没有特定于模型的任何模板文件,这是关键。 What will happen is mvc will use the default template to render that object. 将会发生的事情是mvc将使用默认模板来渲染该对象。

DefaultController.cs (controller) 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 (model) Application.cs(模型)

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

Index.cshtml (view) 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)
            <br/>
        }
    }
</body>

Result Output 结果输出

结果

I found that if I do the following in the template I get the data as expected: 我发现,如果我在模板中执行以下操作,则将按预期方式获取数据:

@model IEnumerable<Tens.Models.Applicant>

@{

foreach(var item in Model)
{
    @Html.DisplayFor(m=>item.Forenames)
    <br />
    @Html.DisplayFor(m=>item.Surname)
    <br />
    <text>----------------------------------</text>
<br />
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么ASP.NET MVC模板视图使用Html.DisplayFor显示不属于模型的数据? - Why do the ASP.NET MVC template views use Html.DisplayFor to display data that is not part of the model? 在razor视图中使用Html.DisplayFor应用CSS类 - Applying css class using Html.DisplayFor inside razor view 如何在MVC 3中编辑Html.DisplayFor方法的CSS? - How do I edit the CSS of the Html.DisplayFor method in MVC 3? Html.Display / Html.DisplayFor / Html.DisplayForModel可以与DataTable一起使用吗? - Can Html.Display/Html.DisplayFor/Html.DisplayForModel work with a DataTable? Html.DisplayFor编码问题到输入值 - Encoding issue with Html.DisplayFor into an input value @ HTML.DisplayFor正在显示不需要的标记 - @HTML.DisplayFor is displaying unwanted markup ASP NET MVC 2 EditorFor和DisplayFor的模板 - ASP NET mvc 2 Template for EditorFor and DisplayFor 如何创建类似于Html.DisplayFor的html帮助器方法,该方法将在自定义文件夹中显示 - How to create a html helper method similar to Html.DisplayFor that will look in a custom folder 如何在@ Html.DisplayFor(m =&gt; m.EventDetailSection.ContactAccountTel)中插入 - How to insert &nbsp in a @Html.DisplayFor(m => m.EventDetailSection.ContactAccountTel) .NET MVC3-从XML读取HTML部件并以razor视图显示 - .NET MVC3 - reading HTML parts from XML and showing in razor view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM