Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I load multiple records from database table in jquery datatable and the process is fine run if i am not assign the ajax call back method i.e. success: function (data) {}.
But when i am assigned, not load the records.
Actually i want to that, if record is not found then new view(i.e. view name: create view) is open otherwise it is load the records into list view(i.e. view name: showGrid view)

Why is it???
Please help me any body......

What I have tried:

ShowGrid.cshtml file:
$(document).ready(function () {
            $("#demoGrid").DataTable({
                processing: true, // for show progress bar
                serverSide: true, // for process server side
                filter: true, // this is for disable filter (search box)
                orderMulti: false, // for disable multiple column at once
                //pageLength: 5,
                paging: false,
                //searching: false,
                //ordering: false,
                scrollY: 200,
                keys: true,
                scroller: {
                    loadingIndicator: true
                },

                ajax: {
                    url: "/Demo/LoadData",
                    type: "POST",
                    datatype: "json",
                    success: function (data) {
                        if (data.recordsTotal == 0)
                            window.location.href = data.redirecturl; // your action should return an object having [redirecturl] property
                    }
                },

                columnDefs:
                    [{
                        targets: [0],//companyID
                        visible: false,
                        searchable: false
                    },
                    {
                        targets: [2],//contactname
                        orderable: false
                    },
                    {
                        targets: [3],//contactTitle
                        orderable: false
                    },
                    {
                        targets: [4],//CIty
                        orderable: false
                    },
                    {
                        targets: [5],//PostalCode
                        orderable: false
                    },
                    {
                        targets: [6],//country
                        orderable: false
                    },
                    {
                        targets: [7],//phone
                        visible: false,
                        searchable: false,
                        orderable: false
                    },
                    ],

                columns: [
                    { data: "CustomerID", name: "CustomerID", autoWidth: true },
                    { data: "CompanyName", name: "CompanyName", autoWidth: true },
                    { data: "ContactName", "title": "ContactName", name: "ContactName", autoWidth: true },
                    { data: "ContactTitle", name: "ContactTitle", autoWidth: true },
                    { data: "City", name: "City", autoWidth: true },
                    { data: "PostalCode", name: "PostalCode", autoWidth: true },
                    { data: "Country", name: "Country", autoWidth: true },
                    { data: "Phone", name: "Phone", title: "Status", autoWidth: true },
                ]
            });
        });

--------------
Demo Controller file is:
public ActionResult ShowGrid()
        {                
            return View();
        }

        public ActionResult Create()
        {

            return View();
        }
    public ActionResult LoadData()
        {
            try
            {
                //Creating instance of DatabaseContext class  
                using (DatabaseContext _context = new DatabaseContext())
                {
                    var draw = Request.Form.GetValues("draw").FirstOrDefault();
                    var start = Request.Form.GetValues("start").FirstOrDefault();
                    var length = Request.Form.GetValues("length").FirstOrDefault();
                    var sortColumn = Request.Form.GetValues("columns[" + Request.Form.GetValues("order[0][column]").FirstOrDefault() + "][name]").FirstOrDefault();
                    var sortColumnDir = Request.Form.GetValues("order[0][dir]").FirstOrDefault();
                    var searchValue = Request.Form.GetValues("search[value]").FirstOrDefault();


                    //Paging Size (10,20,50,100)    
                    int pageSize = (length != null && Convert.ToInt32(length) > 0) ? Convert.ToInt32(length) : 0;
                    int skip = start != null ? Convert.ToInt32(start) : 0;
                    int recordsTotal = 0;

                    // Getting all Customer data
                    var customerData = (from tempcustomer in _context.Customers
                                        where 1==2 select tempcustomer);

                    //Sorting    
                    if (!(string.IsNullOrEmpty(sortColumn) && string.IsNullOrEmpty(sortColumnDir)))
                    {
                        customerData = customerData.OrderBy(sortColumn + " " + sortColumnDir);
                    }
                    //Search    
                    if (!string.IsNullOrEmpty(searchValue))
                    {
                        customerData = customerData.Where(m => m.CompanyName == searchValue);
                    }

                    //total number of rows count     
                    recordsTotal = customerData.Count();
                    //Paging     
                    var data = customerData.Skip(skip).Take(recordsTotal).ToList();

                    //Returning Json Data
                    return Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data });
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
Posted
Updated 16-Oct-19 3:26am

1 solution

Do what the comments already in your code are asking you to do

if (data.recordsTotal == 0)
                            window.location.href = data.redirecturl; // your action should return an object having [redirecturl] property


If recordsTotal is 0 because there are no records it will redirect to the url supplied in the redirecturl parameter of your return data. So look at your return data;

return Json(new { draw = draw, recordsFiltered = recordsTotal, recordsTotal = recordsTotal, data = data })


Is there a "redirecturl" parameter in your return data? No, so work out the url of your "add" page\action that should be shown for no results and return that with your json as a property named redirecturl.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900