2012-09-23 20 views
0

我得到以下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> 
} 
+0

但你硬编码5 ?? – codingbiz

+0

是的,我对“数量”硬编码5,而控制器对该参数确实看到5。 – MooCow

+0

您的提交按钮没有引用特定的行,我认为将提交整个表单而不是单击它的行。我认为应该有一种方法来指定每行的ID和数量,以便提交知道要提交什么值 – codingbiz

回答

0

Codingbiz是正确的。提交按钮不是指任何特定的行,而是整个表。我怀疑“hddId”总是为“1”的原因是因为这是提交表单时看到的第一个“hddId”引用。我改之以@using(Html.BeginForm(...))是@foreach循环内部,每一行都有自己的表,并提交按钮:

@foreach (var item in Model) { 

    <tr> 
     @using(Html.BeginForm("AddToCart","Cart")){ 
     <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> 
} 
+0

虽然这可能起作用,但它不是合法的HTML。 ''只能包含''和'​​'元素。这不能保证它可以在任何浏览器中运行,或者将来在任何浏览器中继续运行。 –

+0

我将@using(...)块移动到块之外,那么它仍然会被认为是非法html格式,因为它仍然在

标记内? – MooCow

+0

否。表标记内唯一有效的元素是标题,col,colgroup,thead,tbody或tr。 @using不会出现在呈现的html中,它只是一种呈现表单标记及其内的所有内容的机制。表格在表格标签内或在tr标签内仅在th和td标签内不合法。 –

0

我通过使用button type =“submit”而不是input type =“submit”来解决验证问题。通过将ID号传递给控制器​​,它仍能正常工作。

@using(Html.BeginForm("AddToCart","Cart")){ 
<table> 
    <tr> 
    //header stuff here 
    </tr> 

    @foreach (var item in Model) { 
    //row data goes here 
    //remove hidden field reference to @item.hddId 
    <tr> 
    <td> 
     <button type="submit" value="@item.hddId" name="hddId">Add to Cart</button> 
    </td> 
    </tr> 
</table> 
} 
相关问题