MVC4绑定模型的ICollection或列表中的部分 [英] MVC4 bind model to ICollection or List in partial

查看:155
本文介绍了MVC4绑定模型的ICollection或列表中的部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于模型

 公共类任务
{
公众诠释TASKID {搞定;组; }
公共字符串名称{搞定;组; }
公众的ICollection< SomeData>信息{搞定;组; }
}

其中,

 公共类SomeData
{
    公众诠释SomeDataId {搞定;组; }
    公共字符串描述{搞定;组; }
}

我有一个观点

  @model myProject.Models.Task< D​​IV>
    @ Html.LabelFor(型号=> model.Title)
< / DIV><表>
@ Html.Partial(_ InformationEdit,Model.Information.ToList(),新的ViewDataDictionary(Html.ViewDataContainer.ViewData){
                TemplateInfo =新System.Web.Mvc.TemplateInfo {HtmlField preFIX =信息}})
< /表>

和我的部分是

  @model的IList< myProject.Models.SomeData>@for(INT I = 0; I< Model.Count();我++)
{
    &所述; TR>
        &所述; TD>
            @ Html.EditorFor(modelItem =>产品型号[I]。说明)
        < / TD>
    < / TR>
 }

然而

我的HTML领域正在呈现像

 <输入类=文本框单行ID =Information__0__DescriptionNAME =信息[0]。说明类型=文本>

凡的名字应该是信息[0]。说明。它有在那里额外的点,所以贴的时候没有被正确绑定回模型。我该如何解决这个问题?

模式结合一个列表 我可以看到我的ID是应该的,但我就是无法找出正确的语法。

此外,有没有一个更优雅的方式与的IEnumerable 使用 @foreach

相关阅读:

<一个href=\"http://stackoverflow.com/questions/12974957/asp-net-mvc4-bind-a-create-view-to-a-model-that-contains-list\">ASP.Net MVC4绑定[建立查看&QUOT;到包含列表模型

<一个href=\"http://stackoverflow.com/questions/653514/asp-net-mvc-model-binding-an-ilist-parameter\">ASP.NET MVC模型绑定一个IList&LT;&GT;参数


解决方案

更改我的偏

  @model的IList&LT; myProject.Models.SomeData&GT;@ {
    VAR信息=型号;
}@for(INT I = 0; I&LT; Information.Count();我++)
{
    &所述; TR&GT;
        &所述; TD&GT;
            @ Html.EditorFor(modelItem =&GT;信息[I]。说明)
        &LT; / TD&GT;
    &LT; / TR&GT;
 }

作品,但是这似乎有点奇怪!

我猜确保所绑定的对象是物业需要绑定到确实有些魔法......其他建议或解释是欢迎的同名!

Given a Model

public class Task
{
public int TaskId { get; set; }
public string Title { get; set; }
public ICollection<SomeData> Information { get; set; }
}

where

public class SomeData
{
    public int SomeDataId { get; set; }
    public string Description { get; set; }
}

I have a view

@model myProject.Models.Task

<div>
    @Html.LabelFor(model => model.Title)
</div>

<table>
@Html.Partial("_InformationEdit", Model.Information.ToList(), new ViewDataDictionary(Html.ViewDataContainer.ViewData) {
                TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = "Information" }})
</table>                    

and my partial is

@model IList<myProject.Models.SomeData>

@for (int i = 0; i < Model.Count(); i++)
{
    <tr>
        <td>
            @Html.EditorFor(modelItem => Model[i].Description)
        </td>
    </tr>
 }

However

My Html fields are being rendered like

<input class="text-box single-line" id="Information__0__Description" name="Information.[0].Description" type="text">

Where the names should be Information[0].Description. It's got an additional dot in there, so is not being bound back to the model correctly when posted. How can I fix this?

As per Model binding to a list I can see what my Id's are supposed to be, but I just can't figure out the correct syntax.

Also, is there a more elegant way to achieve this with an IEnumerable using a @foreach ?

Related:

ASP.Net MVC4 bind a "create view" to a model that contains List

ASP.NET MVC model binding an IList<> parameter

解决方案

Changing my partial to

@model IList<myProject.Models.SomeData>

@{
    var Information = Model;
}

@for (int i = 0; i < Information.Count(); i++)
{
    <tr>
        <td>
            @Html.EditorFor(modelItem => Information[i].Description)
        </td>
    </tr>
 }

Works, but this seems a bit odd!

I guess ensuring that the object being bound is of the same name as the property it needs to be bound to does some wizardry... Other suggestions or explanations are welcome!

这篇关于MVC4绑定模型的ICollection或列表中的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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