Editor - How to call from ASP.NET MVC?

Editor - How to call from ASP.NET MVC?

andyrandyr Posts: 35Questions: 6Answers: 0
edited May 2015 in Free community support

I used http://editor.datatables.net/manual/generator to build a sample for an ASP.NET Web API project. I can make an editable datatable using that.
But I need this to work in an ASP.NET MVC 5 View. This part of the JavaScript I use in the View:

$('#TSSRAvail').DataTable({
                "dom": "Tfrtip",
                "ajax": "/Home/EditData",
                "columns": [ I PUT ALL THE COLUMNS HERE ][   // ,
            });

does run. But using the IE Developer Toolbar Network tab, I see an Ajax request with a Type = "text/html". That does not call my server code in ""/Home/EditData".
I think I need the Ajax request to run as Type = "application/json". I would appreciate any help on this.

Replies

  • allanallan Posts: 61,832Questions: 1Answers: 10,133 Site admin

    Hi,

    You can add the contentType header to the Ajax request by using ajax in its object form:

    ajax: {
      url: '/Home/EditData',
      contentType:"application/json; charset=utf-8"
    }
    

    The object is passed directly through to $.ajax so you can use any of the jQuery options in it (apart from success which should not be provided as DataTables uses that itself!).

    Allan

  • andyrandyr Posts: 35Questions: 6Answers: 0

    I changed the "ajax" part of the .DataTable(...) to:

    "ajax": {
       url: "/Home/EditData"
       contentType: "application/json; charset=utf-8",
       error: function (jqXHR, textStatus, errorThrown)  { 
                    debugger; alert("ajax failed: " + textStatus); 
            }
    }
    

    Part of jqXHR contains these.NET error messages:
    1. Description: HTTP 404. Please review the following URL and make sure that
    it is spelled correctly.../Home/EditData
    2. [HttpException]: A public action method 'EditData' was not found on controller
    'TSSR.Controllers.HomeController'.
    System.Web.Mvc.Controller.HandleUnknownAction(String actionName)

    I don't know what settings I need on the .NET side.
    I posted a similar question at http://forums.asp.net/p/2048602/5903654.aspx?Re+How+to+use+getJSON+in+MVC+5

  • mfahadali_8mfahadali_8 Posts: 1Questions: 0Answers: 0

    Please check below link to see the full tutorial of Datatable.Net server side filtering, sorting, searching by using MVC.
    http://blog.softtechsln.com/post/2015/05/06/mvc-and-datatable-net-1-10-server-side-paging-sorting-and-filtering

  • andyrandyr Posts: 35Questions: 6Answers: 0

    I deleted the attributes I had put above the ASP.NET MVC HomeController EditData method:
    [System.Web.Mvc.HttpPost]
    [System.Web.Mvc.HttpGet]
    It seems that those in ASP.NET MVC mean ONLY allow that type of request. So the first one was blocking the ajax() GET call to "/Home/EditData".

    I think you NEED both those above an ASP.NET Web API Controller method. You may want to note this in https://editor.datatables.net/manual/net/webapi, or any in ASP.NET samples you make.

This discussion has been closed.