简体   繁体   English

MVC4将模型绑定到ICollection或List in partial

[英]MVC4 bind model to ICollection or List in partial

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 我的Html字段被渲染为

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

Where the names should be Information[0].Description . 名称应为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. 根据模型绑定到列表,我可以看到我的Id应该是什么,但我无法弄清楚正确的语法。

Also, is there a more elegant way to achieve this with an IEnumerable using a @foreach ? 另外,使用@foreachIEnumerable是否有更优雅的方法来实现这一目标?

Related: 有关:

ASP.Net MVC4 bind a "create view" to a model that contains List ASP.Net MVC4将“创建视图”绑定到包含List的模型

ASP.NET MVC model binding an IList<> parameter ASP.NET MVC模型绑定IList <>参数

You could use the <input... directly like this: 你可以像这样直接使用<input...

Page: 页:

<table>
    @Html.Partial("_InformationEdit", Model.Information)
</table>

Partial Page: 部分页面:

@for (int i = 0; i < Model.Count(); i++)
{
    <tr>
        <td>
            <input class="text-box single-line" id="Information[@i]Description" name="Information[@i].Description" type="text" value="@Model[i].Description" />
        </td>
    </tr>
}

Or, to be able to pass the prefix as in your example you could keep the Page code the same and change your partial like: 或者,为了能够像您的示例中那样传递前缀,您可以保持页面代码相同并更改您的部分:

Page: 页:

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

Partial Page: 部分页面:

@for (int i = 0; i < Model.Count(); i++)
{
    <tr>
        <td>
            @{
                string fieldName = string.Format("{0}[{1}].Description", ViewData.TemplateInfo.HtmlFieldPrefix, i);
                <input class="text-box single-line" id="@fieldName" name="@fieldName" type="text" value="@Model[i].Description" />
            }
        </td>
    </tr>
}

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! 我想确保绑定的对象与它需要绑定的属性具有相同的名称做一些魔法...欢迎其他建议或解释!

your partial should be: 你的部分应该是:

@model IList<myProject.Models.SomeData>

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

also it's easier to read if you replace i by the item name 如果用项目名称替换i,它也更容易阅读

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

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