Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列出,其中的参数在更改时应来自dropdownlist_C#_Asp.net_Asp.net Mvc_Razor - Fatal编程技术网

C# 列出,其中的参数在更改时应来自dropdownlist

C# 列出,其中的参数在更改时应来自dropdownlist,c#,asp.net,asp.net-mvc,razor,C#,Asp.net,Asp.net Mvc,Razor,我有一个列表类型的视图,模型有一个selectList,它在更改时应该更改actionLink参数 我尝试过使用表单并将模型的所有内容发布到控制器,但它不起作用 这是观点的一部分 @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.coursenumber) </td> <

我有一个列表类型的视图,模型有一个selectList,它在更改时应该更改actionLink参数

我尝试过使用表单并将模型的所有内容发布到控制器,但它不起作用

这是观点的一部分

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

priceKey是从dropdownlist计算出来的。

您需要使用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>

注意:不清楚您显示的代码是否是您发回的表单的一部分,但如果是,则需要使用for循环或自定义EditorTemplate来生成表单控件。使用foreach循环生成重复的名称属性,而不使用索引器,当您发布表单时,索引器将不会绑定到您的模型。

为了响应客户端事件,选择一个选项,您需要使用javascript/jquery。你到底想做什么?你应该在选择/更改下拉项时回发提交表单。正如@StephenMuecke所说,使用javascript更改下拉项更改时的actinlink参数,如$ddlCourse1.bindchange,函数{change params here};'我试图将下拉列表值绑定到ActionLink,这是stephen works给出的解决方案,我通过思考abt回发使自己变得复杂。太棒了!我尝试了带有回发的编辑器模板,但这很简单。
$(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
  });
});