自动保存在MVC(ASP.NET) [英] Autosave in MVC (ASP.NET)

查看:92
本文介绍了自动保存在MVC(ASP.NET)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现Web系统,由我公司管理的一些数据。
我们使用MVC(多specically ASP.NET MVC 4),其中我是完全新的。

I'm implementing a web system to manage some data from my company. We are using MVC (more specically ASP.NET MVC 4), in which I'm completely new to.

我遇到的问题是,我们计划使用自动保存,就像Gmail中。
我们计划使用变更事件排队,并在一段时间后通过AJAX提交更改。
在第一个想到我会使用JavaScript,不知道但如果这对MVC的最佳途径。
那林具有另一问题是,一些用户将输入的信息不是内部的形式,但在表中来代替。
另外,页面的布局有点稀疏,我不相信,我可以换全部投入到一个表单,或者即使我应该这样做。

The problem I'm having is that we planned to use autosave, just like GMail. We were planning on using change events queuing and once in a while submit changes through ajax. On a first thought I would use JavaScript, not sure though if that's the best way for MVC. Another trouble that I'm having is that some of the information the user will enter is not inside forms, but in a table instead. Also the layout of the page is a little sparse and I don't believe I can wrap all the inputs into a single form or even if I should do it.

我的问题是:


  1. 什么是落实使用MVC自动保存最好的方式,我应该用或不用JavaScript的?

  2. 是否有任何的JavaScript库或在ASP.NET MVC功能来实现排队还是应该手工?做

  3. 另外,我可以使用的形式来包装表行?

请注意:我已经看到了一些建议使用localStorage的或其它的客户端持久性,但我需要的是服务器的持久性,我们甚至没有一个保存网页上的按钮

Note: I've seen some suggestions to use localstorage or others client persistency, but what I need is server persistency, and we don't even have a save button on the page.

感谢您提前帮助;)

推荐答案

您可以添加的形式=myformid属性是外在形式要素,包括它在形式上。我将在beggining 数据脏=FALSE属性添加到所有的元素和附着变更事件将改变不断变化的元素,真正的数据脏属性。然后你就可以节省形成例如每30秒(获取数据有变化= TRUE,并传递给服务器元素)。保存后,每一个元素的数据又脏变为假。使用jQuery自动保存的例子:

You can add form="myformid" attribute to elements that are outside form to include it in form. I would add data-dirty="false" attribute to all elements at the beggining and attach change event that would change data-dirty attribute of changing element to true. Then you can save form each 30 seconds for example (get elements that have data-change=true and pass to server). After saving, every element's data-dirty becomes false again. Example of autosave with jQuery:

window.setInterval(function(){
    var dirtyElements = $('#myformid').find('[data-dirty=true]').add('[form=myformid][data-dirty=true]');
    if(dirtyElements.length > 0){
        var data = dirtyElements.serialize();
        $.post('saveurl', data, function(){
            dirtyElements.attr('data-dirty', false);
            alert('data saved successfully');
        });
    }
}, 30000); // 30 seconds

附加事件表格的所有元素:

Attaching event to all elements of form:

$(function(){
    var formElements = $('#myformid')
                           .find('input, select, textarea')
                           .add('[form=myformid]')
                           .not(':disabled')
                           .each(function(){
                                $(this).attr('data-dirty', false).change(function(){
                                    $(this).attr('data-dirty', true);
                                });
                           });
});

这篇关于自动保存在MVC(ASP.NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆