State management in ASP.NET MVC & MVC controls | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (168).NET Core  (29).NET MAUI  (148)Angular  (95)ASP.NET  (51)ASP.NET Core  (81)ASP.NET MVC  (89)Azure  (40)Blazor  (186)BoldSign  (11)DocIO  (24)Essential JS 2  (101)Essential Studio  (194)File Formats  (50)Flutter  (127)JavaScript  (214)Microsoft  (118)PDF  (77)Python  (1)React  (87)Streamlit  (1)Succinctly series  (130)Syncfusion  (755)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (46)Windows Forms  (59)WinUI  (63)WPF  (147)Xamarin  (160)XlsIO  (28)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (134)Chart  (91)Cloud  (14)Company  (445)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (61)Development  (536)Doc  (7)DockingManager  (1)eBook  (98)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (29)Extensions  (21)File Manager  (6)Gantt  (17)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  (420)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (36)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (11)Scheduler  (48)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (29)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (110)UI  (290)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (26)Visual Studio Code  (17)Web  (492)What's new  (270)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.
                    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.


Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top