不用于界面显示模板 [英] Display template not used for interface

查看:89
本文介绍了不用于界面显示模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与显示模板的问题和处理的接口和实现接口的对象。在我有很多目标,我想在一个固定的方式呈现的例子,我决定创建一个接口,在我决定把到共享显示模板文件夹的视图引用此。 DisplayFor 不缝为它们实现了视图界面传递给它的对象的工作,没有任何一个知道一个解决的办法。

I'm having a problem with display templates and dealing with interfaces and objects which implement the interface. In the example I have many objects, which I want to be rendered in a fixed way, I decided to create an interface and reference this in the view which I've decided to put into the shared display templates folder. DisplayFor doesn't seam to work for objects passed to it which implement the interface in the view, does any one know a solution to this.

它可能更容易通过code来解释,所以我已经写了一个简单的例子。底座接口和两个类从它继承:

Its probably easier to explain via code so I've wrote a quick example. The base interface and two classes which inherit from it:

public interface IPet
{
    String Name { get; }
}

public class Dog : IPet
{
    public String Name { get; set; }
}

public class Cat : IPet
{
    public String Name { get; set; }
}

在共享显示模板的例子显示模板

The example display template in shared display templates

@model IPet

<div>@Model.Name</div>

的示例视图模型被传递到视图

The example view model to be passed to the view

public class VM
{
    public IPet Master { get; set; }
    public IEnumerable<IPet> Minions { get; set; }
}

的控制器(在这种情况下,创建模拟信息)

The controller (in this case to create mock information)

    public ActionResult Index()
    {
        var viewModel = new VM();
        viewModel.Master = new Cat(){Name = "Fluffy"};

        var minions = new List<IPet>();

        minions.Add(new Dog(){Name = "Dave"});
        minions.Add(new Dog(){Name = "Pete"});
        minions.Add(new Cat(){Name = "Alice"});

        viewModel.Minions = minions;

        return View(viewModel);
    }

最后,我期望 DisplayFor 工作视图

@model ViewInheritance.Models.VM

<h2>Master</h2>

@Html.DisplayFor(x => x.Master)

<h2>Minions</h2>

@Html.DisplayFor(x => x.Minions)

由于所有的对象都在视图模型的接口定义,howcome它无法使用显示模板?

Given that all the objects are are defined in the view model as the interfaces, howcome it fails to use the display template?

一个解决方案,我发现是简单地用code

One solution I have found is to simply use the code

@Html.DisplayFor(x => x.Master, "IPet")

要回顾一下,问题是:

为什么会出现这种情况?
有没有一种方法,使 DisplayFor 正常工作出一类它实现了 IPET的其实应该寻找在共用共享视图 IPet.cshtml

Why does this happen? Is there a way to make DisplayFor correctly work out that a type of Cat which implements IPet should in fact be looking at the common shared view IPet.cshtml?

感谢

推荐答案

开始一个新的MVC应用程序和解决code实际编译视图渲染罚款。移动视图到共享文件夹时,也呈现不错。

Starting a new MVC application and fixing the code to actually compile the view renders fine. It also renders fine when moving the view into the shared folder.

我添加setter才能IPET:

I Added setter to IPet:

public interface IPet
{
    String Name { get; set; }
}

我更新的实施,并添加公共访问:

I updated implementation and added public accessors:

public class Dog : IPet
{
    public String Name { get; set; }
}

public class Cat : IPet
{
    public String Name { get; set; }
}

我离开你的虚拟机独自也在你查看没有发生任何变化code。
pressing F5,运行MVC应用程序呈现的结果如预期(见图片)。

I left your VM alone and also did not change any code in your View. Pressing F5, running the MVC application rendered the results as expected (See image).

这篇关于不用于界面显示模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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