2015-07-21 19 views
-1

我有一個List類型的視圖,Model有一個selectList,在更改時應該更改actionLink參數。列表<Model>帶有一個參數,該參數應該在更改時從下拉列表中提取

我曾嘗試使用窗體並將模型的所有內容發佈到控制器,但它不起作用。

這是priceKey從下拉列表中計算出的視圖

@foreach (var item in Model) 
{ 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.coursenumber) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.coursename) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.coursescopename) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.avetmiss) 
     </td> 
     <td> 
      @Html.DropDownListFor(modelitem => item.InvoiceOptionsId, item.InvoiceOptionsList, "--select--", new {@class = "form-control", id = "ddl" + item.coursenumber}) 
     </td> 
     <td> 
      @Html.ActionLink("Events","Events",new{id= item.coursenumber,priceKey = item.InvoiceOptionsId}) 
     </td> 
    </tr> 
} 

的一部分。

+0

爲了響應客戶端事件(選擇一個選項),您需要使用javascript/jquery。你究竟在做什麼? –

+1

您應該在選擇/更改下拉項目時回發(提交)表格。 – adatapost

+0

As @StephenMuecke表示使用javascript改變下拉式變化的actinlink參數,如$(「#ddlCourse1」)。bind(「change」,function(){change params here});' – Nilesh

回答

1

您需要使用javascript/jquery來處理客戶端事件。一個辦法是修改HTML來(假定for環 - 見下面的註釋)

<td> 
    @Html.DropDownListFor(m => m[i].InvoiceOptionsId, Model[i].InvoiceOptionsList, "--select--", new { @class = "form-control" }) 
</td> 
<td> 
    <a href="#" class="event" data-id="@Model[i].coursenumber">Events</a> 
</td> 

然後添加下面的腳本(需要jquery{version}.js

$(function() { 
    var url = '@Url.Action("Events")'; 
    $('.event').click(function() { 
    var id = $(this).data('id'); // get the items id 
    var priceKey = $(this).closest('tr').find('select').val(); // get the value of the associated dropdown 
    location.href = url + '?id=' + id + '&priceKey=' + priceKey; // redirect 
    }); 
}); 

注意:它不清楚,如果代碼您顯示的內容是您回發的表單的一部分,但如果是這樣,您需要使用for循環(或自定義EditorTemplate)來生成表單控件。使用foreach循環會生成重複的name屬性(無索引器),當您發佈表單時,這些屬性將不會綁定到您的模型。

+0

完美! 我曾試着用回發的編輯器模板,但這很簡單。 –