dropdownoption更改时更新表

我有一个表的一部分,其值取决于我的下拉列表中的值。 如果下拉列表的选定选项发生更改,如何更新表。

@foreach (var choice in ViewData.Model.Options where choiceId == ViewData.Model.choiceLevelId) {  ..... 

落下

  
@Html.DropDownListFor(model => model.choiceLevelId, ( ...

你在说什么表? ASP.NET MVC不了解桌面或椅子。 它适用于模型,控制器和视图。

因此,您有一个View,其中您已经呈现了一个框,并且您希望用户更改此选择框的值以调用Controller Action将新选择的值传递给服务器,以便它可以执行某些处理。 我对吗?

您必须在视图中使用javascript才能订阅此下拉列表的更改事件。 那么你有几种可能性:

  • 使用AJAX请求将所选值发送到服务器。
  • 将当前浏览器(使用window.location.href )重定向到控制器操作,将所选值作为操作参数传递给它。

根据您的要求,您可以选择最适合您的技术。

让我用一个使用jQuery的例子说明第一种方法:

  

以及将处理AJAX请求的控制器操作:

 [HttpPost] public ActionResult Foo(string value) { // this action will be invoked when the user changes the selection // in one of the dropdown lists and passed the newly selected value. // So using this new value you could perform the desired processing // on your model here and return a view. Since this action was invoked // with an AJAX call you could return a JSON string to the client indicating // the success or failure of whatever processing you were intending to perform // here ... } 

更新:

如果您想在选择更改时提交包含下拉列表的表单,您可以使用以下内容: