如何在MVC中为DropDownListFor实现类似自动回发的功能。目前,在下拉列表中选择一个值后,我必须刷新页面以查看应用于页面的更改。
在视图中,
dropdownlistfor就像
@Html.DropDownListFor(m => m.SelectedItem, Model.MyItemList, new { @id = "DropDownListForId"})
并且onchange事件按原样处理
<script type = "text/javascript">
$(function () {
$('#DropDownListForId').change(function () {
var item = $(this).val();
$.ajax({
url: '@Url.Action("SomeAction", "SomeController")',
type: 'GET',
data: { value: item },
success: function(result) {
}
});
});
});
</script>
谢谢!
答案 0 :(得分:1)
我认为您可以通过在DropDownList
假设myForm
形成id
<script type = "text/javascript">
$(function () {
$('#DropDownListForId').change(function () {
$('form#myForm').submit();
});
});
</script>