MVC3 Ajax调用Controller

MCB*_*urg 2 asp.net-mvc-3

无论如何都要提交表格,但是它仍留在页面上吗?

现在我正在显示一个对象表,但是每一行都有一个可编辑的值,每行都有自己的Ajax表单,但当我点击更新按钮时,它会转到方法,但整个页面都会改变.

Dar*_*rov 7

无论如何都要提交表格,但是它仍留在页面上吗?

当然,你可以使用AJAX:

@using (Html.BeginForm())
{
    ... some form input fields

    <input type="submit" value="Go" />
}
Run Code Online (Sandbox Code Playgroud)

然后在一个单独的文件中不显眼地AJAXify这个表单:

$(function() {
    $('form').submit(function() {
        $.ajax({
            url: this.action,
            type: this.method,
            data: $(this).serialize(),
            success: function(result) {
                // TODO: handle the results of the AJAX call
            }
        });
        return false;
    });
});
Run Code Online (Sandbox Code Playgroud)

并且为了避免编写所有这些javascript代码,您可以查看优秀的jquery.form插件:

$(function() {
    $('form').ajaxForm(function(result) {
        // TODO: handle the results of the AJAX call
    });
});
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用ASP.NET MVC 3 Ajax.BeginForm帮助器:

@using (Ajax.BeginForm(new AjaxOptions { OnSuccess = "success" }))
{
    ... some form input fields

    <input type="submit" value="Go" />
}
Run Code Online (Sandbox Code Playgroud)

然后在javascript中有一个成功处理程序:

function success(result) {
    // TODO: handle the results of the AJAX call
}
Run Code Online (Sandbox Code Playgroud)

如果您想使用帮助程序jquery.unobtrusive-ajax.js,除了jquery页面之外,还需要包含脚本Ajax.*.