我得到以下Razor视图,显示硬盘列表,以便用户添加到购物车中。当用户按下每一行HDD旁边的按钮时,它会将数量和HDD的身份传递给控制器。但是,当我检查控制器的参数时,虽然每个硬盘ID都正确显示,但“hddId”始终为1,“数量”正确5。ASP MVC将模型数据作为表单数据传回
@model IEnumerable<TestStore.Models.Hdd>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@using(Html.BeginForm("AddToCart","Cart")){
<table>
<tr>
<th>
Ident
</th>
<th>
Brand
</th>
<th>
Name
</th>
<th>
Model
</th>
<th>
Speed
</th>
<th>
Capacity
</th>
<th>
Cache
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.hddId)
</td>
<td>
@Html.Hidden("hddId", item.hddId)
@Html.Hidden("quantity", 5)
@Html.DisplayFor(modelItem => item.brand)
</td>
<td>
@Html.DisplayFor(modelItem => item.name)
</td>
<td>
@Html.DisplayFor(modelItem => item.model)
</td>
<td>
@Html.DisplayFor(modelItem => item.speed)
</td>
<td>
@Html.DisplayFor(modelItem => item.capacity)
</td>
<td>
@Html.DisplayFor(modelItem => item.cache)
</td>
<td>
<input type="submit" value="Add to Cart" />
</td>
</tr>
}
</table>
}
但你硬编码5 ?? – codingbiz
是的,我对“数量”硬编码5,而控制器对该参数确实看到5。 – MooCow
您的提交按钮没有引用特定的行,我认为将提交整个表单而不是单击它的行。我认为应该有一种方法来指定每行的ID和数量,以便提交知道要提交什么值 – codingbiz