在ASP.Net MVC中从视图传递到模型时,列表计数为空 [英] List count empty when passing from view to model in ASP.Net MVC

查看:63
本文介绍了在ASP.Net MVC中从视图传递到模型时,列表计数为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开始之前,我已经看过这个问题:

Before I begin, I have looked at this question:

asp.net mvc模型对于< List>的绑定失败编辑器模板中的项目

我有一个看起来像这样的模型:

I have a model which looks as so:

public class PartnerListModel
{
    public List<PartnersModel> Partners { get; set; }

    public PartnerListModel()
    {
        Partners = new List<PartnersModel>();
    }
}

然后我的PartnersModel看起来像这样:

And then my PartnersModel looks as such:

public class PartnersModel
{
    public int ID { get; set; }
    public string Name { get; set; }
    public bool IsActive { get; set; }
}

我将PartnersListModel传递给视图,如下所示:

I'm passing the PartnersListModel to the view, which looks like this:

using (Html.BeginForm("Update", "Partners", FormMethod.Post))
            {
                @Html.EditorFor(m => m.Partners)
                <input type="submit" value="Submit Changes"/>
            }

最后,我的编辑器模板如下所示:

And finally, my editor template looks like this:

@model AdminWebsite.Models.Partners.PartnersModel
<div>
    <span>
        @Html.DisplayFor(m => m.Name)
    </span>
    <span>
        @Html.EditorFor(m => m.IsActive)
    </span>
    @Html.HiddenFor(m => m.ID)
    @Html.HiddenFor(m => m.Name)
</div>

我的控制器的操作是这样,并且代码确实设法执行了此操作:

My controller's action is as so, and the code does actually manage to hit this action:

public ActionResult Update(PartnerListModel partners)

为什么我的模型内的List的计数为0?我找不到我的示例与Stack Overflow上可接受的答案有所不同的任何原因.我有什么遗漏的东西可以解释为什么我传回的数据值没有被添加到列表中吗?

Why is it that my List inside the model has a count of 0? I can't find any reason why my example differs from an accepted answer on Stack Overflow. Is there anything that I am missing that would explain why my data values passed back are not being added to the list?

使用Chrome的开发人员工具,我已经确认自己拥有一个类似于以下内容的列表:

Using Chrome's developer tools I've been able to confirm that I have a list which looks similar to the following:

  • 合作伙伴[0] .IsActive = true
  • 合作伙伴[0] .IsActive = false
  • 合作伙伴[0].名称=嗨"
  • 合作伙伴[0] .ID = 1
  • 合作伙伴[1] .IsActive = false 等
  • Partners[0].IsActive = true
  • Partners[0].IsActive = false
  • Partners[0].Name = "Hi"
  • Partners[0].ID = 1
  • Partners[1].IsActive = false etc.

有什么想法吗?预先感谢.

Any ideas? Thanks in advance.

推荐答案

您正在将List<PartnersModel>传递给action方法,但是该action-method期望使用PartnerListModel.

You're passing a List<PartnersModel> to the action method, but the action-method expects a PartnerListModel.

您应该像这样更改操作方法:

You should change the Action-method like this:

public ActionResult Update(List<PartnersModel> partners)

这样做的原因是默认的ModelBinder实际上并不嵌套图形.如果要访问这两个项目,则需要同时包含两个参数:

The reason for this is that the default ModelBinder doesn't actually nest the graph. If you would want access to both items, you would need to do include both parameters:

public ActionResult Update(PartnerListModel partnerlistmodel, List<PartnersModel> partners){

}

如果要使用嵌套模型,则必须实现自己的modelbinder.

If you want a nested model, you'd have to implement your own modelbinder.

以下问题也有类似的问题:绑定可编辑的子代列表

The following question has a similar problem: Binding an editable list of children

这篇关于在ASP.Net MVC中从视图传递到模型时,列表计数为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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