将模型对象列表列表发布到ASP.NET MVC中的Controller [英] Post list of list of model object to Controller in ASP.NET MVC

查看:76
本文介绍了将模型对象列表列表发布到ASP.NET MVC中的Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样的形式

ViewModel:

public class ProductViewModel
{
    public string Product { get; set; }
    public IEnumerable<SizeColorQuantityViewModel> SizeColorQuantities { get; set; }
}
public class SizeColorQuantityViewModel
{
    public string ColorId { get; set; }
    public List<SizeAndQuantity> SizeAndQuantities { get; set; }
}
public class SizeAndQuantity
{
    public int SizeId { get; set; }
    public int Quantity { get; set; }
}

查看:

@model ProjectSem3.Areas.Admin.Models.ProductViewModel
@{
    ViewBag.Title = "Create";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
    string[] ListColor = { "Red", "Blue" };
    string[] ListSize = { "S", "M", "L", "XL" };
}
    @for (var i = 0; i < ListColor.Length; i++)
    {
    <div class="form-group">
       <label class="col-md-2 control-label">Color:</label>
       <div class="col-md-2">
          @Html.TextBox("[" + i + "].ColorId", null, new { @Value = ListColor[i], @class = "form-control", @readonly = "readonly" })
       </div>
    </div>
    <div class="form-group">
       <label class="col-md-2 control-label">Size and Quantity:</label>
       @for (var j = 0; j < ListSize.Length; j++)
       {
       <div class="col-md-2">
          @Html.TextBox("[" + i + "][" + j + "].SizeAndQuantities.SizeId", null, new
          {
          @class = "form-control",
          @style = "margin-bottom: 15px",
          @Value = ListSize[j],
          @readonly = "readonly"
          })
          @Html.TextBox("[" + i + "][" + j + "].SizeAndQuantities.Quantity", null, new { @class = "form-control" })
       </div>
       }
    </div>
    }

控制器:

// GET: Admin/Product
public ActionResult Create()
{
    return View();
}
// POST: Admin/Product
[HttpPost]
public ActionResult Create(ProductViewModel product, IEnumerable
<SizeColorQuantityViewModel>
sizeColorQuantity, IEnumerable
<SizeAndQuantity>
sizeAndQuantity)
{ 
     return View();
}

我可以获得从ViewModel IEnumerable<SizeColorQuantityViewModel> sizeColorQuantity传递到Controller的值.但是使用此模型IEnumerable<SizeAndQuantity> sizeAndQuantity,我无法获得任何价值. 因为这是二维数组,所以我不知道这个问题.您能教我如何绑定IEnumerable<SizeAndQuantity> sizeAndQuantity.

I can get value which is passed from ViewModel IEnumerable<SizeColorQuantityViewModel> sizeColorQuantity to Controller. But with this model IEnumerable<SizeAndQuantity> sizeAndQuantity, I can't get any value. Cause this is 2-D Array so, I have no idea for this issues. Could you teach me how to bind value for IEnumerable<SizeAndQuantity> sizeAndQuantity.

推荐答案

您生成的输入名称属性与模型没有关系的输入,因此不能受DefaultModelBinder约束.您需要首先在控制器(而不是视图)中生成数据,以便可以绑定到模型.

Your generating inputs with name attributes that have no relationship to your model, therefore cannot be bound by the DefaultModelBinder. You need to start by generating the data in the controller (not in the view) so that you can bind to your model.

以下假设您将SizeColorQuantities属性更改为List<SizeColorQuantityViewModel>

The following assumes you change your SizeColorQuantities property to List<SizeColorQuantityViewModel>

public ActionResult Create()
{
    var colors = new List<string>(){ "Red", "Blue" };
    var sizes = new List<string>(){ "S", "M", "L", "XL" };

    var model = new ProductViewModel()
    {
        Product = "My product",
        SizeColorQuantities = new List<SizeColorQuantityViewModel>
    };
    foreach(var color in colors)
    {
        var child = new SizeColorQuantityViewModel()
        {
            ColorId = color,
            SizeAndQuantities = new List<SizeAndQuantity>
        };
        model.SizeColorQuantities.Add(child);
        foreach(var size in sizes)
        {
            child.SizeAndQuantities.Add(new SizeAndQuantity()
            {
                SizeId = size // assumes SizeId is changed to string, not int
            });
        }
    }
    return View(model);
}

您现在有了一个正确填充的视图模型,该模型将传递给视图,您可以使用嵌套在for循环中的强类型HtmlHelpers绑定到该视图上

You now have a correctly populated view model that will be passed to the view which you can bind to with strongly typed HtmlHelpers inside nested for loops

@model ProductViewModel
....
@using (Html.BeginForm())
{
    ....
    @for(int i = 0; i < Model.SizeColorQuantities.Count; i++)
    {
        @Html.TextBoxFor(m => m.SizeColorQuantities[i].ColorId, new { @class = "form-control", @readonly = "readonly" })
        for (int j = 0; j < Model.SizeColorQuantities[i].SizeAndQuantities .Count; j++)
        {
            @Html.TextBoxFor(m => m.SizeColorQuantities[i].SizeAndQuantities[j].SizeId, new { @class = "form-control", @readonly = "readonly" })
            @Html.TextBoxFor(m => m.SizeColorQuantities[i].SizeAndQuantities[j].Quantity, new { @class = "form-control" })
        }
    }
    <input type="submit" ... />
}

注意:始终使用强类型的***For() HtmlHelper方法,并且从不尝试在使用HtmlHelper方法.

Note: Always use the strongly typed ***For() HtmlHelper methods and never attempt to set the value (or name) attribute when using a HtmlHelper method.

您的POST方法现在需要

Your POST method now needs to be

[HttpPost]
public ActionResult Create(ProductViewModel model)
{
    ....
}

请注意,您也可以按照

Note you can also use custom EditorTemplate's for your types as explained in HTML Table to ADO.NET DataTable, which also explains how your name attributes must be generated in order to bind to collections. In your case, for example your Quantity inputs need to be (compare that to what your currently generating)

<input name="SizeColorQuantities[0].SizeAndQuantities[0].Quantity" ... />
<input name="SizeColorQuantities[0].SizeAndQuantities[1].Quantity" ... />
....
<input name="SizeColorQuantities[1].SizeAndQuantities[0].Quantity" ... />
<input name="SizeColorQuantities[1].SizeAndQuantities[1].Quantity" ... />
....

这篇关于将模型对象列表列表发布到ASP.NET MVC中的Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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