How to Pass JsonResult Data to Controller ActionResult and Extract it?

220 views Asked by At

In my Details view, I have a "Previous" and a "Next" button to navigate between records. The following code is in my Details method:

ViewBag.PreviousId = _db.Applications.OrderByDescending(a => a.AppNumber).Where(a => a.AppNumber < application.AppNumber).Select(a => a.Id).FirstOrDefault();

ViewBag.NextId = _db.Applications.OrderBy(a => a.AppNumber).Where(a => a.AppNumber > application.AppNumber).Select(a => a.Id).FirstOrDefault();

Instead of navigating between all records, I need to only navigate between records shown in the results of my jQuery datatable.

For example, records 1, 2, 3, 4 and 5 may exist in the database. However, after searching/filtering my datatable, the results may just be records 1, 3 and 5. Therefore, if I open the Details view of record 3, clicking the "Previous" button should take me to record 1 and clicking the "Next" button should take me to record 5.

In my Index view, I have a jQuery datatable. The following is part of my JsonResult method that performs searching, sorting and pagination:

public JsonResult GetApplications()
{
    var draw = Request.Form.GetValues("draw")[0];
    var order = Request.Form.GetValues("order[0][column]")[0];
    var orderDir = Request.Form.GetValues("order[0][dir]")[0];
    var start = Convert.ToInt32(Request.Form.GetValues("start")[0]);
    var length = Convert.ToInt32(Request.Form.GetValues("length")[0]);
    var data = _db.Applications.AsQueryable();
    var totalRecords = data.Count();

    --code--

    var filteredRecords = data.Count();

    data = data.Skip(start).Take(length);

    var modifiedData = data.Select(a =>
        new
        {
            a.AppNumber,
            --code--  
            a.Id
        });

    return Json(new
    {
        draw = Convert.ToInt32(draw),
        recordsTotal = totalRecords,
        recordsFiltered = filteredRecords,
        data = modifiedData
    }, JsonRequestBehavior.AllowGet);
}

How can I pass the JsonResult data to my Details method and extract the AppNumber and Id values?

Pseudo code:

ViewBag.PreviousId = GetApplications().OrderByDescending(a => a.AppNumber).Where(a => a.AppNumber < application.AppNumber).Select(a => a.Id).FirstOrDefault();

ViewBag.NextId = GetApplications().OrderBy(a => a.AppNumber).Where(a => a.AppNumber > application.AppNumber).Select(a => a.Id).FirstOrDefault();
2

There are 2 answers

0
Roe On

To answer your question. I don't recommend using Json inside C# methods, it's very useful for sending back to the front-end not back-end since its not as staticly typed as C#.

You should Create a seperate function that is responsible for getting the data from the Applications table with a custom return type. Create a class Application add all your properties to this class and voila you can now use that class for all interactions inside your application.

Would look something like this.

public class Application
{
    public int Draw {get; set;}
    public int RecordCount {get; set;}
    public RecordsFiltered {get; set;}
}

In order to use this class you can simply do this inside a method:

var application = new Application()
{
    Draw = draw,
    RecordCount = recordCount,
    //etc
}

To use the data from this instance of the class you can do this:

application.Draw //to get one specific property from the class

application //to get the whole object

You can use this new class as a return type in a database Application get method:

public List<Application> GetApplications()
{
    var applications = new List<Application>();
    // get all data and fill application object

    return applications
}
0
AudioBubble On

My solution was to save data.ToList() to a session in the JsonResult method:

Session["HR_Applications"] = data.ToList();.

Then grab the values from the session in the Details method:

var applications = (List<Application>)Session["HR_Applications"];

And lastly, set the previousId and nextId values:

ViewBag.PreviousId = applications.OrderByDescending(a => a.AppNumber).Where(a => a.AppNumber < application.AppNumber).Select(a => a.ApplicationId).FirstOrDefault();

ViewBag.NextId = applications.OrderBy(a => a.AppNumber).Where(a => a.AppNumber > application.AppNumber).Select(a => a.ApplicationId).FirstOrDefault();