State management in ASP.NET MVC & MVC controls | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (14)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (66)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (919)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (36)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (150)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (40)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)

State management in ASP.NET MVC and Syncfusion MVC controls

State management in ASP.NET MVC is an interesting throwback to the core concept of statelessness in HTTP—wherein there is no default support for state persistence. Compare this to ASP.NET Web Forms, which by default used ViewState to store the state for each control.

There are many benefits associated with stateless pages, or when state is maintained only on the client browser, via a cookie for example:

· Sites can easily scale to a cluster of servers in a server farm without having to worry about maintaining session state.

· Debugging problems produced by session expiry will not be an issue.

· Cached pages can be put to better use, as most of the page content might stay the same in the absence of session state.

· A session is easy to recover if the server crashes when state is persisted on the client rather than the server.

The MVC team clearly had the above in mind when creating a default, stateless architecture.

Though there is no default support for state persistence in MVC, developers can use several options to enable state persistence across page post-backs:

· Session state.

· ASP.NET cache.

· Cookies.

Each of the above has its own pros and cons, depending on the scale of the deployment. By letting developers build their own persistence logic, ASP.NET MVC encourages developers to follow best practices in ensuring minimalist memory and bandwidth usage, which has been sorely lacking in Web Forms, considering the common abuse of ViewState with caching large, unnecessary information.

State Persistence in Syncfusion Controls

Syncfusion ASP.NET MVC controls adhere to these principles and are stateless by default. However, as a control becomes more complex, like a tree or a grid, it becomes interactive; there is always a necessity to maintain the current visual state of the control somewhere. We facilitate this by including APIs in JavaScript that allow you to retrieve and apply the state of the controls from and into the cookies.

State Persistence in Syncfusion MVC Grid

State persistence in the Grid control can be maintained by using JQuery cookies. This is achieved by saving the expand-and-collapse state of each page in the cookies and rendering the Grid control with reloaded cookies. Here is the code snippet that does this:

var pagingInformation = $.cookie('PagingInfo'); // Define the variable using cookies. Using this variable, store the expand-and-collapse state information of each page.  
pagingInformation = [];
var count = 0;
var indValue;
function ActionBegin(sender, args) {
    // The expand-and-collapse state information is saved when switching over to another page.
    if (args.requestType != "Refresh") {    // To avoid adding pagingInformation while the grid refreshes.
        var element = $find("EntityGrid");
        if ($("#EntityGrid").find(".RecordPlusCollapse").length > 0) {
            var collapsedRows = $("#EntityGrid").find(".RecordPlusCollapse"); // Find all the collapsed state td’s of the current page.
            var indexValue = [];
            $.each(collapsedRows, function (index, rows) {
                indexValue.push($(rows).parent("tr").index())  // Find the parent of the collapsed state (td) and store the parent (tr) index into the array.
            });
            // Get the previous page number from this command.
            var pageNo = element._pager._prevPageNo;
            // If the pagingInformation length is greater than zero.
            if (pagingInformation.length > 0) {
                // This loop is used to check whether the corresponding page information is already stored in the cookies variable.                    
                $.each(pagingInformation, function (index, item) {
                    if (pagingInformation[index][0].PageNo == pageNo) {
                        count = 1;
                        indValue = index;
                    }
                });
                // If the previous page information already exists, then simply replace the current collapsed state details.
                if (count == 1)
                    pagingInformation[indValue][0].CollapsedRowIndex = indexValue;
                    // If the previous page information does not already exist, then simply add PageNo and collapsed state details into the cookies variable.
                else
                    pagingInformation.push([{ "PageNo": pageNo, "CollapsedRowIndex": indexValue }]);
            }
                // Initially, pagingInformation is empty, so we can add PageNo and the corresponding collapsed state row (tr) index value in the cookies variable. 
            else {
                pagingInformation.push([{ "PageNo": pageNo, "CollapsedRowIndex": indexValue }]);
            }
        }
        count = 0;
        //  this JSON.stringify is used to serialize the array.
        $.cookie('PagingInfo', JSON.stringify(pagingInformation));
    }
}
function ActionComplete(sender, args) {
    // Applying the saved state on the grid.
    //Maintained the state while paging and grid refresh.
    //If we click Next Page or refresh the page, then this function is executed.
    if (args.RequestType == "paging" || args.RequestType == "Refresh") {
        var element = $find("EntityGrid");
        // Get currentPageNo from this command.
        var currpageNo = element._pager._currentPageNo;

        // De-serialize the cookies variable data by using the $.parseJSON() command.
        var currentPagingInformation = $.parseJSON($.cookie('PagingInfo'));
        if (currentPagingInformation != null) {
            $.each(currentPagingInformation, function (index, item) {
                if (item[0].PageNo == currpageNo) {
                    $.each(item[0].CollapsedRowIndex, function (ind, i) {
                        // Get the corresponding page information details from the cookies variable and assign it to statePersistenceExpandCollapse. Then, the page is rendered with the expanded-and-collapsed state.
                        $find("EntityGrid").statePersistenceExpandCollapse(i);
                    });
                }
            });
        }
    }
}

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed