在ASP.NET MVC 3模板编辑器IEnumerable的模型属性 [英] IEnumerable model property in an ASP.NET MVC 3 Editor Template

查看:312
本文介绍了在ASP.NET MVC 3模板编辑器IEnumerable的模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个IEnumerable属性模型(警告伪code遵循)

 公共类PersonModel {
    公共字符串名称{;组; }
    公共IEnumerable的< AddressModel>地址{搞定;组; }
}公共类AddressModel {
    公共字符串名称{;组; }
}

我要显示的地址在同一视图子对象

Person.cshtml

  @model PersonModel<形式为GT;
    < H2>与人LT; / H>    @ Html.EditorFor(M = GT; m.Name)    &所述; UL> @ Html.EditorFor(M => m.Addresses)所述; / UL>< /表及GT;

EditorTemplate / AddressModel

  @model AddressModel<立GT; @ Html.TextboxFor(M = GT; m.Name)LT; /李>

非凡,一切工作正常。

但是,作为一个Git,我现在要开始使用模板布局来换我的标准型方式的东西。

所有属性

因此​​,我创建string.cshtml和list.cshtml做到这一点,用一个_Control.cshtml的布局

EditorTemplates / _Control.cshtml

 < D​​IV>
    @ Html.Label(的String.Empty)
    < D​​IV CLASS =输入>
        @RenderBody()        @ Html.ValidationMessage(的String.Empty)
    < / DIV>
< / DIV>

EditorTemplates / string.cshtml

  @model串
@ {
    布局=_Control.cshtml;
}
@ Html.TextBox(的String.Empty,型号)

(到目前为止耶!但等待...哦不...)

这里的麻烦

 < UL> @ Html.EditorFor(M = GT; m.Addresses)LT; / UL>

从主视图(见上文)成为

  @ Html.EditorFor(M = GT; m.Addresses,列表)

EditorTemplates / list.cshtml

  @model IEnumerable的<对象>
@ {
    布局=_Control.cshtml;
}
< UL>
    @foreach(以型号VAR项){
        @ Html.EditorFor(M = GT;项)
    }
< / UL>

此呈现的ID和名称错误,类似Addresses_item_Name,其不包含的Id,因此添加的ID与用于循环

  @for(VAR I = 0; I< Model.Count();我++){
    @ Html.EditorFor(M => Model.ElementAt(i))的
}

这炸毁的MVC前pression帮手不允许任何东西,但阵列,但地址必须是IEnumerable的<>,因为EF4.1不支持.ToArray一个子查询,即在

  VAR模型=(上接第在DataContext.People
             其中,p.Id = 1
             选择新PersonModel {
                 名称= p.Name,
                 地址=(来自于p.Addresses
                              选择新AddressModel {
                                  NAME = a.Name
                              })。ToArray的()// **不支持的**
             })FirstOrDefault()。

有没有人碰到这个?
是否有做标准的方式?

这工作,但它是正确的?

EditorTemplates / list.cshtml

  @model IEnumerable的<对象>
@ {
    布局=_Control.cshtml;
    VAR preFIX = ViewData.TemplateInfo.HtmlField preFIX;
}
< UL>
    @for(VAR I = 0; I< Model.Count();我++){
        变种项= Model.ElementAt(ⅰ);
        ViewData.TemplateInfo.HtmlField preFIX =的String.Format({0} [{1}],preFIX,我);
        @ Html.EditorFor(M = GT;的项目,空,的String.Empty)
    }
< / UL>

期望的结构是

 <形式为GT;
    <控制>
       人名控制元件
    <控制>    <控制>
       地址列表控制元件
    <控制>
< /表及GT;


解决方案

该EditorFor不允许放东西的属性类型,而不是方法,这将是它为什么快死了。

我的建议是创建模板可以通过一个地址的项目,然后使用一个循环之外编辑出每一个,或使用editorformodel它会替你。

I have a model which has an IEnumerable property (warning pseudo-code to follow)

public class PersonModel {
    public string Name { get; set; }
    public IEnumerable<AddressModel> Addresses { get; set; }
}

public class AddressModel {
    public string Name { get; set; }
}

I want to display the Address sub-objects in the same view

Person.cshtml

@model PersonModel

<form>
    <h2>Person</h2>

    @Html.EditorFor(m=>m.Name)

    <ul>@Html.EditorFor(m=>m.Addresses)</ul>

</form>

EditorTemplate/AddressModel

@model AddressModel

<li>@Html.TextboxFor(m=>m.Name)</li>

Marvellous, all works fine

But, being a git, I now want to start using Template Layouts to wrap all my properties in a standard type way and stuff

So I create a string.cshtml and a list.cshtml to do this, with a _Control.cshtml for the layout

EditorTemplates/_Control.cshtml

<div>
    @Html.Label(string.Empty)
    <div class="input">
        @RenderBody()                    

        @Html.ValidationMessage(string.Empty)
    </div>
</div>

EditorTemplates/string.cshtml

@model string
@{    
    Layout = "_Control.cshtml";
}
@Html.TextBox(string.Empty, Model)

(so far yay! but wait.. oh no..)

Here's the trouble

<ul>@Html.EditorFor(m=>m.Addresses)</ul>

from the main view (see above) becomes

@Html.EditorFor(m=>m.Addresses, "List")

EditorTemplates/list.cshtml

@model IEnumerable<object>
@{    
    Layout = "_Control.cshtml";
}
<ul>
    @foreach(var item in Model){
        @Html.EditorFor(m => item)
    }
</ul>

This renders the id and names incorrectly, something like Addresses_item_Name, which does not contain the Id, so adding the id with a for loop

@for (var i = 0; i < Model.Count();i++ ) {
    @Html.EditorFor(m => Model.ElementAt(i))
}

This blows up as the MVC expression helper does not allow anything but arrays, but Addresses has to be IEnumerable<> because EF4.1 does not support .ToArray inside a sub-query ie.

var model = (from p in DataContext.People
             where p.Id = 1
             select new PersonModel {
                 Name = p.Name,
                 Addresses = (from a in p.Addresses
                              select new AddressModel {
                                  Name = a.Name 
                              }).ToArray() // **NOT SUPPORTED**
             }).FirstOrDefault();

Has anyone come up against this? Is there are standard way to do it?

This works, but is it right?

EditorTemplates/list.cshtml

@model IEnumerable<object>
@{    
    Layout = "_Control.cshtml";
    var prefix = ViewData.TemplateInfo.HtmlFieldPrefix;
}
<ul>
    @for (var i = 0; i < Model.Count();i++ ) {
        var item = Model.ElementAt(i);
        ViewData.TemplateInfo.HtmlFieldPrefix = string.Format("{0}[{1}]",prefix, i);
        @Html.EditorFor(m => item, null, string.Empty)
    }
</ul>

The desired structure is

<form>
    <control>
       Person Name Control Elements
    <control>

    <control>
       Address List Control Elements
    <control>
</form>

解决方案

The EditorFor doesn't allow anything put property types, not methods, this will be why it is dying.

My advice would be to create the template as a single address item, then use a loop outside to edit out each one, or use editorformodel which does that for you.

Si

这篇关于在ASP.NET MVC 3模板编辑器IEnumerable的模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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