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();
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
Applicationadd 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.
In order to use this class you can simply do this inside a method:
To use the data from this instance of the class you can do this:
You can use this new class as a return type in a database
Applicationget method: