你是怎么做到的AutoPostBack在asp.net mvc的web表单的功能?

问题描述:

我有一个简单的WebGrid其显示列表。我也有一个包含几个项目的组合框。我想,当用户更改选择,更改的值应发布到服务器。我怎样才能做到这一点?

I have a simple webgrid which displays list. I also have a combobox which contains few items. I want that when user changes the selection, the changed value should be posted to the server. How can I do this?

任何code段将是有益的。

Any code snippets would be helpful.

在此先感谢:)

自动回传Web窗体与一些JavaScript来实现。这不是外的现成的MVC,但是足够简单,做你自己的。

Auto-postback in Web Forms was accomplished with some JavaScript. This is not out-of-the-box in MVC, but simple enough to do on your own.

假设你有jQuery的:

Assuming you have jQuery:

$(document).ready(function() {
    $('#someCheckBox').change(function() {
        $('#yourFormId').submit();
    });
});

这是最接近的Web窗体怎么经典作品;基本上没有当与someCheckBox的ID的复选框被选中或取消选中,提交表单ID为yourFormId'。当然,你可以将其更改您的需求。

This is "closest" to how classic Web-Forms work; basically does "When a checkbox with the ID of 'someCheckBox' is checked or unchecked, submit the form with id 'yourFormId'. You can of course change this to your needs.

这是不包括外的开箱,由于大多数开发商倾向于AJAX调用,而不是全面的后背上,我会鼓励你,如果可以做到的。什么可能是更preferable是:

This wasn't included out-of-the-box due to most developers favoring AJAX calls instead of full-blown post-backs, which I would encourage you to do if possible. What might be more preferable is:

 $(document).ready(function() {
    $('#someCheckBox').change(function() {
        $.ajax(/*make an AJAX call*/);
    });
});