使用jQuery + ASP.NET MVC自动保存表单输入

Gop*_*ath 7 asp.net-mvc jquery

我们希望实现一个自动保存内容的网络表单.类似于gmail/google docs的东西自动保存功能.

有人可以建议如何使用ASP.NET MVC + jQuery实现这一点吗?

Dar*_*rov 7

jQuery Forms插件和ASP.NET MVC操作应该完成这项工作:

public ActionResult SaveDraft(FormCollection form)
{
    // TODO: Save the form values and return a JSON result 
    // to indicate if the save went succesfully
    return Json(new { success = true });
}
Run Code Online (Sandbox Code Playgroud)

在您的视图中,只需定期调用此操作:

setInterval(function() {
    $('#theFormToSave').ajaxSubmit({ 
        url    : 'SaveDraft',
        success: function (data, textStatus) {
            if (data.success) {
                alert('form successfully saved');
            }
        }
    });
}, 30000);
Run Code Online (Sandbox Code Playgroud)