简体   繁体   English

ASP MVC将视图中的模型数据作为表单数据传递回

[英]ASP MVC passing model data in view back as form data

I got the following Razor view that display a list of HDD so an user and add to a cart. 我得到了以下Razor视图,该视图显示了HDD列表,以便向用户添加并添加到购物车。 When the user press the button next to each row of HDD, it will pass the quantity and HDD's identity to a controller. 当用户按下HDD每行旁边的按钮时,它将把数量和HDD的身份传递给控制器​​。 However, while each HDD's ID does display properly, "hddId" is always 1 and "quantity" is correctly 5 when I inspect the Controller's parameters. 但是,尽管每个HDD的ID都可以正确显示,但是当我检查控制器的参数时,“ hddId”始终为1,“ quantity”正确为5。

@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>
}

Codingbiz is correct. Codingbiz是正确的。 The submit button wasn't referring to any particular row but the entire table. 提交按钮不是指向任何特定的行,而是指向整个表。 I suspect the reason why "hddId" was always "1" is because that's the first reference of "hddId" it see when the form is submitted. 我怀疑“ hddId”始终为“ 1”的原因是因为这是提交表单时看到的“ hddId”的第一个引用。 I changed it so @using(Html.BeginForm(...)) is inside the @foreach loop so each row has its own form and submit button: 我将其更改为@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>
}

As others have said, this doesn't work because when you submit, you're submitting every row in the table, not just the one clicked. 正如其他人所说,这是行不通的,因为在您提交时,您正在提交表中的每一行,而不仅仅是单击的行。

One option is to do what they suggest in this article: 一种选择是执行他们在本文中建议的操作:

http://federmanscripts.com/2010/01/12/form-and-table-row-nesting-workaround/ http://federmanscripts.com/2010/01/12/form-and-table-row-nesting-workaround/

That is, using javascript, you copy the form values on button press to a hidden row and submit that. 也就是说,使用javascript,您可以将按钮按下时的表单值复制到隐藏行并提交。

I got around the validation problem by using button type="submit" instead of input type="submit". 我通过使用按钮type =“ submit”而不是输入type =“ submit”解决了验证问题。 It still works correctly by passing the id number to the controller. 通过将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>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM