ASP.net Core MVC - Postback

Dave KIlby
Dave KIlby used Ask the Experts™
on
I have two text boxes where user can select dates, then click the submit button to populate a webgrid, this works, but when I click on the sort or the filter the dates in the texboxes are not passed back and reset to current date - how can I keep the dates so I can utilize the sort, filters.

here is the code i am using

VIEW
@model IGrid<rptEODTelco>


@{
    ViewData["Title"] = "EOD Telco Summary";

}

<head>
    <link href="~/css/mvc-grid/mvc-grid.css" rel="stylesheet">
</head>

<div class="col-md-12">
    <div class="form-group">
        <form action="/Reports/EODTelcoSummary" method="post">
            <label for="StartDate">Start Date:</label>
            <input type="date" name="StartDate" value="@ViewBag.StartDate" />
            <label for="EndDate">End Date:</label>
            <input type="date" name="EndDate" value="@ViewBag.EndDate" />
            <button type="submit">Submit</button>
        </form>
    </div>
</div>

<div class="col-md-12">
    <div class="panel panel-primary list-panel">
        <div class="panel-heading list-panel-heading">
            <table align="center" width="100%">
                <tr>
                    <td align="center" colspan="2"><span style="font-size:28px;">EOD Telco Summary</span></td>
                </tr>
            </table>

        </div>
    </div>
    <div class="class=" panel-body">
        @(new HtmlGrid<rptEODTelco>(Html, Model))
    </div>
</div>

<script src="~/js/mvc-grid/mvc-grid.js"></script>
<script>
    [].forEach.call(document.getElementsByClassName('mvc-grid'), function (element) {
        new MvcGrid(element);
    });
</script>

Open in new window


CONTROLLER
        public ActionResult EODTelcoSummary(string StartDate, string EndDate)
        {
            if (isNullOrEmpty(StartDate))
            {
                StartDate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
                EndDate = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
            }

            ViewData["StartDate"] = StartDate;
            ViewData["EndDate"] = EndDate;

            return View(CreateEODTelcoSummaryGrid(StartDate, EndDate));

        }

        private IGrid<rptEODTelco> CreateEODTelcoSummaryGrid(string StartDate, string EndDate)
        {
            IGrid<rptEODTelco> grid = new Grid<rptEODTelco>(rptRepository.GetEODTelcoSummaryByDate(Convert.ToDateTime(StartDate), Convert.ToDateTime(EndDate)));
            grid.ViewContext = new ViewContext { HttpContext = HttpContext };
            grid.Query = Request.Query;

            grid.Columns.Add(model => model.date.ToString("dddd, yyyy-MM-dd")).Titled("Date");
            grid.Columns.Add(model => model.program).Titled("Program");
            grid.Columns.Add(model => model.calls).Titled("# of Calls").Filterable(false).Css("text-center"); ;
            grid.Columns.Add(model => model.ABA.ToString("P")).Titled("ABA").Filterable(false).Css("text-center");
            grid.Columns.Add(model => model.SL.ToString("P")).Titled("Service Level").Filterable(false).Css("text-center");
            grid.Columns.Add(model => model.ASA).Titled("ASA").Filterable(false).Css("text-center"); ;

            grid.Id = "grdEODSummary";

            var oppcount = grid;

            foreach (IGridColumn column in grid.Columns)
            {

                column.Filter.IsEnabled = true;
                column.Sort.IsEnabled = true;

            }

            return grid;
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chief Technology Ninja
Distinguished Expert 2018
Commented:
Hi Dave,

Postbacks are gone.. YAY (There is a huge essay I can write here, but I will keep it short. MVC - got rid of Postback and brought ASP.Net close to the HTTP protocol's design - being stateless).

So, when you send something back to server, you have couple of options, you can maintain sort order and other such details on  client side (works but can be tempered with) OR you can pass them back to the server and server can then process and send it back to the client.

Looks tough? it is very easy to implement. I am assuming you are building a .net core app here. If not, couple of things might change but the idea remains the same.

I do see you have couple of pieces in place but if your "posting", your method should be marked with [HttpPost] annotation. Can you please add it to the appropriate method.

Also, you can refer to this sample: https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/sort-filter-page?view=aspnetcore-2.2

Regards,
Chinmay.

Author

Commented:
Thank you for the explanation and example
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Glad I could help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial