jquery - MVC 3 - 通过jquery提交表单并部分页面刷新以显示发布后的结果

标签 jquery asp.net-mvc-3

我在 MVC 3 中有一个表单。目前我正在执行以下操作来发布该表单。 表单正在发布,并且正在进行全页面刷新以显示结果。

提交后,控件将发送状态成功或失败。就是这样。 我只需要根据返回的状态在 div 中显示成功或失败消息。

有人知道如何在 MVC 3 中执行此操作的高级步骤吗?

<div id="ShowResultHere"></div>

@using (Html.BeginForm("Update", "Test", FormMethod.Post, new { id="frmUpdate"}))
{
   //form fields
   <button type="submit" class="sprt bg_red bt_red h27">Update</button>
}

[HttpPost]
public ActionResult Update(TestModel model)
{
   return Json(new { s = "Success" });
}

我希望 Success 静静地出现在 ShowResultHere div onsuccess 回调中。

这可以在 MVC 3 中完成吗?

最佳答案

您可以订阅表单的 submit 事件并执行 AJAX 调用,如下所示:

$(function() {
    $('#frmUpdate').submit(function() {
        $.ajax({
            url: this.action,
            type: this.method,
            data: $(this).serialize(),
            success: function(result) {
               // The AJAX call succeeded and the server returned a JSON 
               // with a property "s" => we can use this property
               // and set the html of the target div
               $('#ShowResultHere').html(result.s);
            }
        });
        // it is important to return false in order to 
        // cancel the default submission of the form
        // and perform the AJAX call
        return false;
    });
});

关于jquery - MVC 3 - 通过jquery提交表单并部分页面刷新以显示发布后的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6640684/

相关文章:

javascript - ChartJS 悬停工具提示颜色未显示正确的颜色

asp.net-mvc-3 - 使用 Intranet 应用程序实现用户角色的最佳方法

linq - ASP.NET MVC 2/.NET 4/Razor - 不能在 ViewModel 成员上使用 Any() 扩展方法

c# - ASP.NET MVC3 : Displaying Validation Errors from Child View in Parent View

javascript - 使用 Greasemonkey 仅定位非事件按钮

javascript - 如何将 div 包裹在多个相同类的元素周围

jquery - 如何将模式窗口置于 Facebook 应用程序的中心?

javascript - Jquery Onclick 事件函数第二次不起作用

Jquery 对话框和带有 IValidatableObject mvc3 的部分 View

javascript - 附加 Div 标签无法正常工作