我有一个表的一部分,其值取决于我的下拉列表中的值。如果下拉列表的选定选项发生更改,如何更新表格。
格
@foreach (var choice in ViewData.Model.Options where choiceId == ViewData.Model.choiceLevelId)
{
<tr class='gridrow'>
<td>.....
下拉
<div class="editor-field">
@Html.DropDownListFor(model => model.choiceLevelId,
(
...
答案 0 :(得分:3)
你在谈论什么表? ASP.NET MVC不了解桌面或椅子。它适用于模型,控制器和视图。
所以你有一个View,你已经在其中呈现了一个<select>
框,你希望用户更改这个选择框的值,以调用一个Controller Action将新选择的值传递给服务器,以便它可以执行一些处理。我是对的吗?
您必须在视图中使用javascript才能订阅此下拉菜单的更改事件。那么你有几种可能性:
window.location.href
)重定向到控制器操作,并将所选值作为操作参数传递给它。根据您的要求,您可以选择最适合您的技术。
让我用一个使用jQuery的例子说明第一种方法:
<script type="text/javascript">
$(function() {
// subscribe to the .change event of the dropdowns
$('select').change(function() {
// fetch the newly selected value
var selectedValue = $(this).val();
// send it as an AJAX request to some controller action
$.post('@Url.Action("Foo")', { value: selectedValue }, function(result) {
// this will be invoked in the case of success of the AJAX call
// TODO: do something with the results returned by the controller action
});
});
});
</script>
以及将处理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
...
}
更新:
如果您想在选择更改时提交包含下拉列表的表单,您可以使用以下内容:
<script type="text/javascript">
$(function() {
// subscribe to the .change event of the dropdowns
$('select').change(function() {
// trigger the submission of the containing form:
$(this).closest('form').submit();
});
});
</script>