简体   繁体   English

使用jQuery和Json在ASP.NET MVC中保存小部件的状态

[英]Save state of Widgets in ASP.NET MVC using jQuery and Json

I am using ASP.NET MVC in C# 我在C#中使用ASP.NET MVC

I have a page where the user can move different Widgets around the page, and I now need a method to save the state of the widgets. 我有一个页面,用户可以在页面上移动不同的窗口小部件,现在需要一种方法来保存窗口小部件的状态。 I am using jQuery in the HTML page, and the jQuery posts the new page layout using JSON. 我在HTML页面中使用jQuery,而jQuery使用JSON发布新的页面布局。 I am unsure how to read the JSON in the controller. 我不确定如何在控制器中读取JSON。

The code I'm using is based on this example here - http://webdeveloperplus.com/jquery/saving-state-for-collapsible-drag-drop-panels/ , but the code for saving the result is in PHP. 我使用的代码基于此处的示例-http: //webdeveloperplus.com/jquery/saving-state-for-collapsible-drag-drop-panels/ ,但是用于保存结果的代码在PHP中。

jQUERY 查询

<script type="text/javascript" >
$(function () {
    $('.dragbox')
.each(function () {
    $(this).hover(function () {
        $(this).find('h2').addClass('collapse');
    }, function () {
        $(this).find('h2').removeClass('collapse');
    })
    .find('h2').hover(function () {
        $(this).find('.configure').css('visibility', 'visible');
    }, function () {
        $(this).find('.configure').css('visibility', 'hidden');
    })
    .click(function () {
        $(this).siblings('.dragbox-content').toggle();
        //Save state on change of collapse state of panel
        updateWidgetData();
    })
    .end()
    .find('.configure').css('visibility', 'hidden');
});

    $('.column').sortable({
        connectWith: '.column',
        handle: 'h2',
        cursor: 'move',
        placeholder: 'placeholder',
        forcePlaceholderSize: true,
        opacity: 0.4,
        start: function (event, ui) {
            //Firefox, Safari/Chrome fire click event after drag is complete, fix for that
            if ($.browser.mozilla || $.browser.safari)
                $(ui.item).find('.dragbox-content').toggle();
        },
        stop: function (event, ui) {
            ui.item.css({ 'top': '0', 'left': '0' }); //Opera fix
            if (!$.browser.mozilla && !$.browser.safari)
                updateWidgetData();
        }
    })
.disableSelection();
});

function updateWidgetData() {
    var items = [];
    $('.column').each(function () {
        var columnId = $(this).attr('id');
        $('.dragbox', this).each(function (i) {
            var collapsed = 0;
            if ($(this).find('.dragbox-content').css('display') == "none")
                collapsed = 1;
            //Create Item object for current panel
            var item = {
                id: $(this).attr('id'),
                collapsed: collapsed,
                order: i,
                column: columnId
            };
            //Push item object into items array
            items.push(item);
        });
    });
    //Assign items array to sortorder JSON variable
    var sortorder = { items: items };

    //Pass sortorder variable to server using ajax to save state
    $.post('/Widgets/SaveLayout', 'data=' + $.toJSON(sortorder), function (response) {
        if (response == "success")
            $("#console").html('<div class="success">Saved</div>').hide().fadeIn(1000);
        setTimeout(function () {
            $('#console').fadeOut(1000);
        }, 2000);
    });
    alert(sortorder);
}

I am willing to consider alternative ways to do this, as I may not have chosen the best way to do this. 我愿意考虑其他替代方法,因为我可能没有选择最佳方法。

Why not use a cookie? 为什么不使用Cookie? This would save you from having to pull that data back and forth from the server so much. 这将使您不必从服务器上来回拉动大量数据。

Phil Haack's blog post http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx specifically handles the problem you are trying to solve and it works great. Phil Haack的博客文章http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx专门处理了您要解决的问题而且效果很好。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM