calculate data in Submit, use in Grid

nkat

Member
Joined
Jun 29, 2022
Messages
18
Programming Experience
1-3
Hello!
Please, consider a simple .NET CORE app.
Controller
Controller:
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TelerikAspNetCoreApp1.Models;

namespace TelerikAspNetCoreApp1.Controllers
{
    public class HomeController : Controller
    {
        public IEnumerable<OrderViewModel> Records { get; set; }
        private bool dataready;


        public IActionResult Index()
        {
            dataready = false;
            return View();
        }

        [HttpPost]
        public IActionResult Index(string ServerIP)
        {
            Records = Enumerable.Range(0, 50).Select(i => new OrderViewModel
            {
                OrderID = i,
                Freight = i * 10,
                OrderDate = new DateTime(2016, 9, 15).AddDays(i % 7),
                ShipName = "ShipName " + i,
                ShipCity = "ShipCity " + i
            });
            dataready = true;
            return View();
        }
        public ActionResult Orders_Read([DataSourceRequest] DataSourceRequest request)
        {
            if (dataready)
            {
                var dsResult = Records.ToDataSourceResult(request);
                return Json(dsResult);
            }
            return (null);
        }

    }
}

View
View:
<form id="Index" method="POST" asp-action="Index">
    <div class="k-w-500">
        <kendo-textbox value="172.16.23.197" name="ServerIP" style="width: 100%" placeholder="Server IP">
        <textbox-label content="Enter IP of the server"/>
        </kendo-textbox>
    </div>
    <div class="form-group form-inline row pull-right">
        <div class="col-sm-10 ">
            <button value="Save" style="margin:0 5px;" class="btn btn-navy">Save</button>
        </div>
    </div>
<input type="hidden" id="selectedCheckboxes" name="selectedCheckboxes">
</form>


@(Html.Kendo().Grid <TelerikAspNetCoreApp1.Models.OrderViewModel>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.OrderID).Filterable(false);
        columns.Bound(p => p.Freight);
        columns.Bound(p => p.OrderDate).Format("{0:MM/dd/yyyy}");
        columns.Bound(p => p.ShipName);
        columns.Bound(p => p.ShipCity);
    })
    .Pageable()
    .Sortable()
    .Scrollable()
    .Filterable()
    .HtmlAttributes(new { style = "height:550px;" })
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .Read(read => read.Action("Orders_Read", "Home"))
        )
)

The app runs and if a user clicks “Save” button then
public IActionResult Index(string ServerIP) fires, IEnumerable<OrderViewModel> Records gets set and a flag dataready is up. But then the public ActionResult Orders_Read([DataSourceRequest] DataSourceRequest request) event fires (in a separate thread creating therefore a new Controller instance and as such -> “dataready” variable equals false and Records are no longer available). And this is the problem I'm trying to overcome.
So, the question is. Would someone suggest a way to fill into the Grid data that has just been calculated in the POST event? “Orders_Read” event should only be used to browse between pages
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,698
Location
Chesapeake, VA
Programming Experience
10+
I think you are assuming that a Controller is like an old fashioned WebForms page where the WebForms framework simulates an extended lifetime of a page by making use of hidden fields on the page to keep session state. This was the first thing that the very first version of MVC dropped. It got rid of that fake Windows desktop like behavior and exposed the web developer to the truth of the statelessness of HTTP. An MVC controller lives only long enough to service a single HTTP request: a single POST, GET, PUT, DELETE, UPDATE, etc.

Consider what would happen if it did live longer than a single request, how would you deal with your app running on 2 or more machines behind a load balancer where the first request goes to machine A and then a follow up request goes to machine B?

So the fix is to not save the state in the controller, but in some other place. This maybe a Redis cache, a database, a memory cache, etc.
 

nkat

Member
Joined
Jun 29, 2022
Messages
18
Programming Experience
1-3
Thank you! So right! I've figured that statelessness hard way
As this app is to be used by a single user, I moved to a static class Model for an in between sessions data storage.
To serve more than one, as you rightly mentioned, I will have to store grid data in somewhere. Redis cache that I didn't know anything about 3 minutes ago seems promising )
 
Top Bottom