部分视图中的mvc表单复制选定的值

时间:2013-12-23 13:00:27

标签: asp.net-mvc asp.net-mvc-4

我有一个MVC应用程序,我使用以下命令将表单输出到屏幕:

@foreach (CarbonFootprintHelper helper in SessionHelper.CarbonFootprint.SelectedItems)
{
    @Html.Partial("_CarbonCalculator_Form", helper)
}

部分具有以下代码:

<form action="@UrlUtilities.Urls.CarbonCalculator?SelectedProductId=@SessionHelper.CarbonFootprint.LoadedProduct.ProductID" method="post" class="carbon-form">
    @Html.ValidationMessage(string.Format("error-{0}", currentIndex))

    @if (SessionHelper.CarbonFootprint.SelectedItems.Count > 1)
    {
        <div class="form-row">
            <input type="submit" name="remove" value="Remove" class="remove-button button" />
        </div>
    }

    <div class="form-row">
        <label for="product-@currentIndex" class="hide-element">Product</label>
        @Html.DropDownList("footprintId", ParentList, "Please select a product", new { id = string.Format("product-{0}", currentIndex), @class = "drop-down" })
    </div>

    @if (ChildList != null)
    {
        <div class="form-row">
            <label for="child-product-@currentIndex" class="hide-element">Sub-Product</label>
            @Html.DropDownList("childFootprintId", ChildList, "Please select a sub-product", new { id = string.Format("child-product-{0}", currentIndex), @class = "drop-down" })
        </div>
    }

    <div class="form-row">
        <label for="quantity-@selectedParentId" class="hide-element">Quantity</label>
        @Html.TextBox("quantity", quantity, new { id = string.Format("quantity-{0}", currentIndex), @class = "textbox" })
        @Html.Raw(Model.Message)
    </div>

    <div class="form-row">
        @Html.Hidden("currentIndex", currentIndex, new { id = string.Format("index-{0}", currentIndex) })
        <input type="submit" name="calculate" value="Calculate" class="button" />
    </div>
</form>

这基本上只是根据我是否选择了它们来显示一些下拉菜单。

我的问题是,如果我有多个表单附加到页面,当我按下计算按钮来更新该表单时,它将更改所有表单中的所有选定值和数量 - 即使我单步执行代码它正在分配正确的值。

有谁知道可能导致这种情况的原因?我有一种感觉,可能是由于输入都具有相同的名称。如果是这样,我如何为每个输入设置不同的名称,但只保留一个控制器 - 对于eaxmple,当前表单发布到:

public ActionResult CarbonCalculator(string selectedProductId, int currentIndex, int footprintId = 0, int childFootprintId = 0, int quantity = 0)

但是如果我有不同的输入名称,那么我如何避免每个表单需要一个新的控制器?

2 个答案:

答案 0 :(得分:0)

我设法完成这项工作的唯一方法是完全删除html帮助程序,并通过手动编码输入来上学:

<form action="@UrlUtilities.Urls.CarbonCalculator?SelectedProductId=@SessionHelper.CarbonFootprint.LoadedProduct.ProductID" method="post" class="carbon-form">
    @Html.ValidationMessage(string.Format("error-{0}", currentIndex))

    @if (SessionHelper.CarbonFootprint.SelectedItems.Count > 1)
    {
        <div class="form-row">
            <input type="submit" name="remove" value="Remove" class="remove-button button" />
        </div>
    }

    <div class="form-row">
        <label for="product-@currentIndex" class="hide-element">Product</label>
        <select name="FootprintId" id="product-@currentIndex" class="drop-down">
            <option @Html.Raw(Model.FootprintId == 0 ? "selected=\"selected\"" : string.Empty)>Please select a product</option>
            @foreach (CarbonFootprintProduct prod in CarbonFootprint.Items.FilteredProducts)
            {
                <option value="@prod.CarbonFootprintID" @Html.Raw(Model.FootprintId == prod.CarbonFootprintID ? "selected=\"selected\"" : string.Empty)>@prod.ProductDescription</option>
            }
        </select>
    </div>

    @if (Model.FootprintId > 0 && Model.SelectedProduct.Children.Count > 0)
    {
        <div class="form-row">
            <label for="child-product-@currentIndex" class="hide-element">Sub-Product</label>
            <select name="ChildFootprintId" id="child-product-@currentIndex" class="drop-down">
                <option @Html.Raw(Model.ChildFootprintId == 0 ? "selected=\"selected\"" : string.Empty)>Please select a sub-product</option>
                @foreach (CarbonFootprintChild child in Model.SelectedProduct.Children)
                {
                    <option value="@child.CarbonFootprintChildID" @Html.Raw(Model.ChildFootprintId == child.CarbonFootprintChildID ? "selected=\"selected\"" : string.Empty)>@child.Description</option>
                }
            </select>
        </div>
    }

    <div class="form-row">
        <div class="infield-holder">
            <label for="quantity-@currentIndex" title="Quantity" class="infield">Qty</label>
            <input type="text" name="Quantity" value="@quantity" class="textbox" id="quantity-@currentIndex" />
        </div>
    </div>

    @Html.Raw(Model.Message)

    <div class="form-row">
        <input type="hidden" name="CurrentIndex" value="@currentIndex" id="index-@currentIndex" />
        <input type="submit" name="calculate" value="Calculate" class="button" />
    </div>
</form>

答案 1 :(得分:-1)

啊,好像我明白了。您应该使用DropDownListFor而不是DropDownList

    @Html.DropDownListFor(Model.FootprintId, ParentList, "Please select a product", new { id = string.Format("product-{0}", currentIndex), @class = "drop-down" })

Model.FootprintId应包含所选产品的ID。

最诚挚的问候, nZ上