带有控制器列表的MVC POST模型 [英] MVC POST Model with List to Controller

查看:55
本文介绍了带有控制器列表的MVC POST模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这两个视图模型

public class PersonViewModel
{
    public int PersonId { get; set; }
    public string PersonName { get; set; }
    public int PersonAge {get; set;}

    public virtual PersonJobSplitViewModel JobSplit { get; set; } // hold each split
    public virtual List<PersonJobSplitViewModel> JobSplits { get; set; } //contain all splits
}

public class PersonJobSplitViewModel
{
    public int PersonJobSplitId { get; set; }
    public int PersonId { get; set; }
    public string JobRole { get; set; }
    public decimal SplitPercentage { get; set; }
    public virtual PersonViewModel PersonViewModel { get; set; }
}

每个人可以拥有1-3个工作.

Each person can have between 1 - 3 jobs.

我有一个绑定到我的 PersonViewModel 的创建视图,并且在控制器的GET方法中,我创建了一个 List< PersonJobSplitViewModel> 的实例,其容量为3,将其分配给 PersonViewModel.JobSplits

I have a Create view which is bound to my PersonViewModel and in my controller's GET method I am creating an instance of List<PersonJobSplitViewModel> with a capacity of 3 and assigning it to PersonViewModel.JobSplits

@model MySolution.Web.ViewModels.PersonViewModel

...

@for (var i = 0; i < Model.JobSplits.Capacity; i++)
{
    @Html.EditorFor(model => model.JobSplit.JobRole);
    @Html.EditorFor(model => model.JobSplit.SplitPercentage);
}

这将使角色和百分比输入呈现给视图3次.我的POST方法期望使用 PersonViewModel ,但是 PersonViewModel.JobSplits 将以 null 的形式出现.正如我所期望的那样, JobSplit 属性包含我的3个拆分之一.

This results in the role and percentage inputs being rendered to the view 3 times. My POST method is expecting a PersonViewModel however PersonViewModel.JobSplits is coming in as null. The JobSplit property contains one of my 3 splits, as I kind of expect.

那我该如何将绑定了JobSplits完整列表的模型发布到控制器上?

我发现以前已经回答过类似的问题,但是我似乎找不到与 MVC5 相关的直接解决方案,并且该解决方案与将列表标记为大型模型有关,因为该模型仅用于传递控制器的列表.

I've found similar things have been answered previously but I cant seem to find a straight forward solution that is relevant to MVC5 and relates to tagging a list to a large model as apposed to just passing a list to the controller.

更新

我现在已经尝试执行以下操作

I've now tried doing the following

@for (var i = 0; i < Model.JobSplits.Capacity; i++)
{
    @Html.EditorFor(model => model.JobSplits[i].JobRole);
    @Html.EditorFor(model => model.JobSplits[i].SplitPercentage);
}

但是我得到了

索引超出范围.必须为非负数且小于集合.参数名称:索引

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

推荐答案

发布后立即解决.

控制器

//Prep view Model
PersonViewModel viewModel = new PersonViewModel();
List<PersonJobSplitViewModel> instantiatedSplitCount = new List<PersonJobSplitViewModel>(3);

for (int i = 0; i < 3; i++)
{
    instantiatedSplitCount.Add(null);
}

viewModel.JobSplits = instantiatedSplitCount;
return View(viewModel);

查看

@for (var i = 0; i < Model.JobSplits.Count(); i++)
{
    @Html.EditorFor(model => model.JobSplits[i].JobRole);
    @Html.EditorFor(model => model.JobSplits[i].SplitPercentage);
}

似乎要检查容量是问题,在我通过之前为列表分配3个空值,然后检查计数才是解决方案.

It seems doing a check on capacity was the problem, assigning 3 nulls to the list before I pass through, and then checking the count was the solution.

在我的控制器中不太漂亮,但是如果其他人可以提出更句法的解决方案,请分享.

Not pretty in my Controller but if anyone else can suggest a more syntactic solution please share.

这篇关于带有控制器列表的MVC POST模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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