形式是這樣的: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 嗎?(它會讓它更容易) –