UIHint不使用EditorTemplate [英] UIHint not using EditorTemplate

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

问题描述

我有一个这样的模型:

public class MyModel {
    [ScaffoldColumn(false)]
    public int CharityId { get; set; }

    [UIHint("Charities")]
    public SelectList Charities { get; set; }
}

然后我有一个称为Charities.cshtml的EditorTemplate:

Then I have an EditorTemplate called Charities.cshtml:

@model MyModel

@Html.DropDownListFor(model => model.CharityId, Model.Charities)

然后在我的页面中

@model MyModel

@Html.EditorForModel()

但是,无论如何,它都不会呈现慈善"模板.我一直在为此绞尽脑汁,它应该可以工作..但事实并非如此.

However, no matter what, it doesn't render the Charities template. I've been wracking my brain on this, and it should work.. but it's not.

有什么想法吗?

问题在于,即使存在UIHint,默认的Object模板也不会呈现除自身之外的复杂对象(所谓的浅潜).我的印象是UIHint可以使它呈现模板,但是我显然是错的.

The issue is that the default Object template will not render a complex object other than itself (so called shallow dive), even if there is a UIHint. I was under the impression that a UIHint would make it render the template, but I was apparently wrong.

解决方法是不要尝试让模型呈现自身.真可悲.

The fix is to not try to let the model render itself. That is sad.

推荐答案

页面中首先出现的第一件事@EditoFormodel()看起来很奇怪.我猜你还有别的意思.

First things first @EditoFormodel() in your page looks bizarre. I guess you meant something else.

现在为止:您已经用UIHint装饰了Charities属性.此属性的类型为SelectList.这意味着Charities.cshtml模板应严格键入SelectList,而不是像以前那样键入MyModel.您可以简单地删除此UIHint属性并具有以下视图模型:

Now to the point: you have decorated the Charities property with UIHint. This property is of type SelectList. This means that the Charities.cshtml template should be strongly typed to SelectList, not to MyModel as you did. You could simply remove this UIHint attribute and have the following view model:

public class MyModel {
    [ScaffoldColumn(false)]
    public int CharityId { get; set; }

    public SelectList Charities { get; set; }
}

并在您看来:

@model MyModel
@Html.EditorForModel()

,然后在~/Views/Shared/EditorTemplates/MyModel.cshtml内部具有:

@model MyModel
@Html.DropDownListFor(model => model.CharityId, Model.Charities)

这是标准约定.如果您希望将编辑器模板命名为Charities.cshtml,则可以在页面中执行以下操作:

That's the standard conventions. If you want your editor template to be called Charities.cshtml you could do this in your page:

@model MyModel
@Html.EditorForModel("Charities")

但是通常您会使用以下模型:

But usually you would have the following model:

public class FooViewModel
{
    [UIHint("Charities")]
    public MyModel Charities { get; set; }
}

现在,如果您将主视图强类型输入为FooViewModel,则可以:

Now if your main view is strongly typed to FooViewModel you can:

@model FooViewModel
@Html.EditorFor(x => x.Charities)

这将呈现Charities.cshtml编辑器模板.

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

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