locked
.net core 3 , and old school MVC Ajax calls? RRS feed

  • Question

  • User-1877975950 posted

    Is it possible to use old school MVC view, JavaScript, Ajax, post call to controller?<br>
    <br>
    I ask because I can only get the strongly typed ' latest' version working.<br>
    <br>
    I think this is worth pursuing to do quick data Actions to the server returning JSON data in a SPA.<br>
    <br>
    I like to use scaffolding for getting started but once the page is loaded, I like to use HTML onclick JavaScript to tweak the data, which I've used to tweak complex scientific data capture.<br>
    <br>
    thanks<br>
    Stew
    Thursday, October 24, 2019 5:52 PM

Answers

  • User475983607 posted

    I get a 404 from the page when I run the Ajax below.

    The URL is incorrect.  Controller is assumed by convention plus you specifically set the route to [Route("/Home/GetTraceTaskData")]

    url: '/Home/GetTraceTaskData',

    Setting async to false is not recommended.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 15, 2019 2:23 PM

All replies

  • User475983607 posted

    Is it possible to use old school MVC view, JavaScript, Ajax, post call to controller?

    Yes.  Have your tried and are receiving an error?  Can you provide the code and the error?

    I ask because I can only get the strongly typed ' latest' version working.

    Can you clarify?  Code helps.

    Thursday, October 24, 2019 5:56 PM
  • User-474980206 posted

    as javascript and ajax are client based, there is no difference with MVC core. what is different, is asp.net core actions/webapi defaults to json & form posts, and does not have xml/soap support builtin. if your client code used xml/soap, you will need to add xml support (see docs) or switch the client code to use json (like all client code written in the last several years).

    also mvc core actions work like mvc 5 webapi and more strict on post data mapping. see [FromBody] and [FromUrl] attributes. The old MVC actions converted all post data to a name/value collection, and then used this for binding. this allowed data binding to work from url or body for each parameter. with mvc core, the binding is formal, its from the url, or the body, and the bode is either form encoded, json or multipart.

    also mvc core defaults to camel casing rather than pascal casing for json.

    Thursday, October 24, 2019 7:14 PM
  • User1634355159 posted

    Hi Stewa11,

    Could you show us your specific problem?In .NET Core 3, We could still use JavaScript, Ajax, post call to controller as before.

    Best Regards,

    Lewis

    Friday, October 25, 2019 9:55 AM
  • User-1877975950 posted

    I get a 404 from the page when I run the Ajax below.

    OK many thanks for assuring me that the old fashioned MVC View/Controller Get/Post actions will work.

    I would like to pass in and receive text or JSON data.

    Here is my JavaScript/Ajax call in the View:

    $.ajax({

    type: "POST",
    data: JSON.stringify(dataIn),
    url: '/HomeController/GetTraceTaskData',
    contentType: "application/json; charset=utf-8",

    dataType: "json",
    traditional: true,
    async: false,
    success: function (result) {

    //pass back result to Client

    },
    failure: function (response) {

    alert(response.responseText);
    },
    error: function (xhr, ajaxOptions, thrownError) {

    var alerttext = xhr.status + xhr.responseText + thrownError;
    alert(alerttext);
    alert(xhr.status); alert(xhr.responseText);
    alert(thrownError);
    }
    });

    Here is my Controller:

    [HttpPost]
    [Route("/Home/GetTraceTaskData")]
    [AcceptVerbs("Post")]
    public string GetTraceTaskData()
    {

    //do some stuff with the 

    return("somestring";

    }

    Friday, November 15, 2019 1:37 PM
  • User475983607 posted

    I get a 404 from the page when I run the Ajax below.

    The URL is incorrect.  Controller is assumed by convention plus you specifically set the route to [Route("/Home/GetTraceTaskData")]

    url: '/Home/GetTraceTaskData',

    Setting async to false is not recommended.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 15, 2019 2:23 PM