形式是这样的:https://gyazo.com/289a1ac6b7ecd212fe79eec7c0634574帖子列表
视图模型:
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();
}
,我可以得到它通过从视图模型IEnumerable<SizeColorQuantityViewModel> sizeColorQuantity
到控制器值。但是用这个型号IEnumerable<SizeAndQuantity> sizeAndQuantity
,我无法获得任何价值。 因为这是二维数组,所以我不知道这个问题。您能否教我如何将值绑定为IEnumerable<SizeAndQuantity> sizeAndQuantity.
与名称属性您生成输入不涉及到你的模型,但不是很多这段代码正在感。为什么你的视图中有'ListColor'和'ListSize'集合?您是否在尝试编辑红/小,红/中,红/大等产品的数量,并再次针对蓝/小,蓝/中等等进行编辑? –
@StephenMuecke是的,我也想过很多方式来发布大小,颜色和数量的产品。你有没有其他的方法?如果你不介意,你能更多地解释我吗?非常感谢:D –
您需要进行很多更改才能完成此项工作。你可以改变IEnumerable到'List 吗?(它会让它更容易) –