Form like this
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; }
}
View:
@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>
}
Controller:
// 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();
}
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.