Postar lista de lista de objeto de modelo no Controller no ASP.NET MVC
Forma como esta:https://gyazo.com/289a1ac6b7ecd212fe79eec7c0634574
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; }
}
Visão:
@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>
}
Controlador:
// 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();
}
Posso obter o valor que é passado do ViewModelIEnumerable<SizeColorQuantityViewModel> sizeColorQuantity
para o controlador. Mas com este modeloIEnumerable<SizeAndQuantity> sizeAndQuantity
, Não consigo obter nenhum valor. Como essa é a matriz 2-D, não tenho idéia para esses problemas. Você poderia me ensinar como vincular valor paraIEnumerable<SizeAndQuantity> sizeAndQuantity.