将ASP.NET MVC中的模型对象列表发布到Controller

时间:2016-08-21 07:55:53

标签: asp.net-mvc-5

这样的表格

enter image description here

视图模型:

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,我无法获得任何价值。 因为这是2-D阵列所以,我不知道这个问题。你能教我如何绑定IEnumerable<SizeAndQuantity> sizeAndQuantity.

的价值

1 个答案:

答案 0 :(得分:6)

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

以下假设您将SizeColorQuantities属性更改为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);
}

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

@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方法,从不尝试设置value(或{{ 1}})使用name方法时的属性。

您的POST方法现在需要

HtmlHelper

请注意,您也可以使用HTML Table to ADO.NET DataTable中所述的自定义[HttpPost] public ActionResult Create(ProductViewModel model) { .... } 类型,这也解释了如何生成EditorTemplate属性以便绑定到集合。在您的情况下,例如您的name输入需要(将其与您当前生成的输入进行比较)

Quantity