我有我使用以下方法來輸出的形式來在屏幕MVC應用程序:MVC形式局部視圖重複選擇的值
@foreach (CarbonFootprintHelper helper in SessionHelper.CarbonFootprint.SelectedItems)
{
@Html.Partial("_CarbonCalculator_Form", helper)
}
局部有以下代碼:
<form action="@[email protected]per.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="[email protected]" 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="[email protected]" 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="[email protected]" 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)
但如果我有不同的輸入名字,然後我怎麼避免需要爲每個表單創建一個新控制器?
我沒有看到你的'
_when我按計算按鈕來更新form_ 當你按它計算的更新整個頁面,而不僅僅是一個表格。它將* current *形式的值發送到服務器,並且服務器返回整個頁面。當您在第二個表格上更改產品並按下'計算'(在第二個表格上)時會發生什麼?你在服務器上收到什麼? – nZeus