2013-12-23 27 views
1

我有我使用以下方法來輸出的形式來在屏幕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) 

但如果我有不同的輸入名字,然後我怎麼避免需要爲每個表單創建一個新控制器?

+0

我沒有看到你的'

'標籤。在您的例子中,你應該有'每個塊 – nZeus

+0

'標籤抱歉,表單標籤包裝的部分 - 我只是還沒有顯示出它上面 – Pete

+0

_when我按計算按鈕來更新form_ 當你按它計算的更新整個頁面,而不僅僅是一個表格。它將* current *形式的值發送到服務器,並且服務器返回整個頁面。當您在第二個表格上更改產品並按下'計算'(在第二個表格上)時會發生什麼?你在服務器上收到什麼? – nZeus

回答

0

我設法使這項工作的唯一方法是通過handcoding輸入完全消除HTML輔助,去老同學:

<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> 
     <select name="FootprintId" id="[email protected]" 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="[email protected]" class="hide-element">Sub-Product</label> 
      <select name="ChildFootprintId" id="[email protected]" 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="[email protected]" title="Quantity" class="infield">Qty</label> 
      <input type="text" name="Quantity" value="@quantity" class="textbox" id="[email protected]" /> 
     </div> 
    </div> 

    @Html.Raw(Model.Message) 

    <div class="form-row"> 
     <input type="hidden" name="CurrentIndex" value="@currentIndex" id="[email protected]" /> 
     <input type="submit" name="calculate" value="Calculate" class="button" /> 
    </div> 
</form> 
-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上

+0

DropDownListFor的值,所以您可以使用該模型 - 我試過以下,但仍是相同的錯誤: @ Html.DropDownListFor(X => x.FootprintId,ParentList, 「請選擇一個產品」,新的{ID =的String.Format( 「產品 - {0}」,CURRENTINDEX), @class =「drop-down」}) – Pete

+0

'ParentList'是否有一個Value ='Model.FootprintId'的項目? – nZeus

+0

是的,這將是列表呈現時選定的選項 – Pete