简体   繁体   English

UIHint不使用EditorTemplate

[英]UIHint not using EditorTemplate

I have a model like this: 我有一个这样的模型:

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

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

Then I have an EditorTemplate called Charities.cshtml: 然后,我有一个名为Charities.cshtml的EditorTemplate:

@model MyModel

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

Then in my page: 然后在我的页面中:

@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. 我一直在为此绞尽脑汁,它应该可以工作..但事实并非如此。

Any ideas? 有任何想法吗?

EDIT: 编辑:

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. 问题在于,即使存在UIHint,默认的Object模板也不会呈现除自身之外的复杂对象(所谓的浅潜)。 I was under the impression that a UIHint would make it render the template, but I was apparently wrong. 我的印象是UIHint将使它呈现模板,但是我显然是错误的。

The fix is to not try to let the model render itself. 解决方法是不要尝试让模型自己渲染。 That is sad. 真伤心

First things first @EditoFormodel() in your page looks bizarre. 首先,页面中的@EditoFormodel()首先看起来很奇怪。 I guess you meant something else. 我想你的意思是别的。

Now to the point: you have decorated the Charities property with UIHint . 到现在为止:您已经用UIHint装饰了Charities属性。 This property is of type SelectList . 此属性的类型为SelectList This means that the Charities.cshtml template should be strongly typed to SelectList , not to MyModel as you did. 这意味着应该将Charities.cshtml模板强类型输入为SelectList ,而不是像您一样键入MyModel You could simply remove this UIHint attribute and have the following view model: 您可以简单地删除此UIHint属性并具有以下视图模型:

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

    public SelectList Charities { get; set; }
}

and in your view: 并且在您看来:

@model MyModel
@Html.EditorForModel()

and then inside ~/Views/Shared/EditorTemplates/MyModel.cshtml have: 然后在~/Views/Shared/EditorTemplates/MyModel.cshtml具有:

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

That's the standard conventions. 这是标准约定。 If you want your editor template to be called Charities.cshtml you could do this in your page: 如果要将编辑器模板命名为Charities.cshtml ,可以在页面中执行以下操作:

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

But usually you would have the following model: 但是通常您将具有以下模型:

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

Now if your main view is strongly typed to FooViewModel you can: 现在,如果将主视图强类型FooViewModel ,则可以:

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

which will render the Charities.cshtml editor template. 这将呈现Charities.cshtml编辑器模板。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM