IEnumerable的在我的视图模型不与EditorForModel显示 [英] IEnumerable in my ViewModel is not displayed with EditorForModel

查看:421
本文介绍了IEnumerable的在我的视图模型不与EditorForModel显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[Validator(typeof(ProdutoCategoriaValidator))]
public class ProdutoCategoriaViewModel
{
    [HiddenInput(DisplayValue = false)]
    public Guid ID { get; set; }

    public IEnumerable<SelectListItem> Tipos { get; set; }  // <<<<-------  Is not showing in my view

    [AdditionalMetadata("data-bind", "event: { change: function(data) { Link(data.Nome());  }}")]
    public string Nome { get; set; }

    [DataType(DataType.Url)]
    [AdditionalMetadata("Prefixo", "Produtos/{tipo-de-produto}#")]
    public string Link { get; set; }

    public int? Ordem { get; set; }

    public ProdutoCategoriaViewModel()
    {
        ID = Guid.NewGuid();
    }
}

解决方案

Solution

@model ProdutoCategoriaViewModel
@using (Html.BeginForm(null, null, FormMethod.Post, new { id="form-produtocategoria", data_bind = "submit: salvar" }))
{
    @Html.AntiForgeryToken()

    <legend>@Html.MvcSiteMap().SiteMapTitle()</legend>

    <fieldset>
        @Html.ValidationSummary(false, "Verifique os erros abaixo:")
        @Html.EditorForModel()
    </fieldset>

    <div class="buttons">
        @Html.ActionLink("Cancelar", "Index")
        <input type="submit" value="SALVAR" />
    </div>
}

SelectListItem.cshtml

@model IEnumerable<SelectListItem>

@Html.DropDownListFor(m => m, Model)

<p>Test</p>

结果

完整映像: http://i.imgur.com/I7HxA.png


  • 我试图把属性UIHint,但仍然显示没什么!

我在做什么错了?

推荐答案

在默认情况下,当你使用 Html.EditorForModel 别指望向下递归这样的复杂属性因为它的类型是你的 Tipos 属性的IEnumerable&LT; SelectListItem&GT; 。布拉德·威尔逊在他的<一个解释了这href=\"http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html\"相对=nofollow>博客文章(更具体地说阅读浅潜水与深潜对帖子的末尾部分)。你需要写的对象类型的自定义编辑模板,如果你希望这种事情发生。

By default when you use Html.EditorForModel don't expect this to recurse down to complex properties such as your Tipos property which is of type IEnumerable<SelectListItem>. Brad Wilson explained this in his blog post (more specifically read the Shallow Dive vs. Deep Dive section towards the end of the post). You will need to write a custom editor template for the Object type if you want this to happen.

另一种可能性是指定模板名称:

Another possibility is to specify the template name:

@Html.EditorFor(x => x.Tipos, "SelectListItem")

还要记住,对于你的编辑模板 SelectListItem 是错误的,因为你的DropDownListFor绑定到模型作为第一个参数。不要忘了,这个助手的第一个参数必须是将被用于保存选定值标量属性。您需要在此您的视图模型字符串或整型属性。再$ P $,第二个参数psents集合。

Also bear in mind that your editor template for the SelectListItem is wrong because you are binding the DropDownListFor to the model as first argument. Don't forget that the first argument of this helper must be a scalar property that will be used to hold the selected value. You need a string or integer property on your view model for this. The second argument represents the collection.

有关编辑模板的另一个重要方面是,当你有的IEnumerable&LT类型的属性; T&GT; 键,名为 T.cshtml <编辑模板/ code>这个编辑器模板必须是强类型的 T 类,而不是的IEnumerable&LT; T&GT; 作为你做你的 SelectListItem.cshtml 模板。如果你使用UIHint或指定模板名称作为第二个参数EditorFor帮手,这并不适用。 n这个情况下,模板将被输入到集合中。

Another important aspect about editor templates is that when you have a property of type IEnumerable<T> and an editor template called T.cshtml this editor template must be strongly typed to the T class and not IEnumerable<T> as you did with your SelectListItem.cshtml template. This doesn't apply if you use UIHint or specify the template name as second argument to the EditorFor helper. n this case the template will be typed to the collection.

因此​​,要回顾一下,你既可以实现自定义对象编辑器模板布拉德·威尔逊认为,将递归下降到复杂的属性,或者您可以修改 _Formulario.cshtml 视图指定EditorFor每个元素。

So to recap, you could either implement a custom object editor template as Brad Wilson suggested that will recurse down to complex properties or you could modify your _Formulario.cshtml view to specify EditorFor each individual elements.

这篇关于IEnumerable的在我的视图模型不与EditorForModel显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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