locked
how to use ajax from html file (without cshtml view) RRS feed

  • Question

  • User1868852945 posted

    For my MVC application I have been using ajax calls to the Contoller to call databse stored procedures.  My MVC appliation also uses a simple html file which I want to add an ajax call to.  Is it possible and How can I do  this without a Controller class?  

    Thanks

    Monday, February 26, 2018 10:25 PM

Answers

  • User-1838255255 posted

    Hi gunderj,

    According to your description, you want to get the data through the AJAX call in html page, i think you could call controller action from the html page Ajax directly, it could return data. view doesn't need corresponding controller, model.  i have test it in my side, it works. 

    Like this:

    $.ajax({
      url: "/controllername/actionname",
      ......
    });

    if my understanding isn't right, please correct me! 

    Best Regards,

    Eric Du 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 1, 2018 8:49 AM

All replies

  • User-1838255255 posted

    Hi gunderj,

    According to your description, as far as i know and i make a test, we can not call method without controller in MVC, we usually put the method in the controller, then call it directly. 

    Could you tell us what is your actual needs and why not use the regular method to call method? 

    Best Regards,

    Eric Du 

    Tuesday, February 27, 2018 6:19 AM
  • User1868852945 posted

    Here is my actual need - my web app has several MVC pages, each with razor page and the two classes for Model and Controller.  In the front of this app is index.html which is not MVC, ie., no razor or Model or Controller classes.  I want to add ajax call to the index.html javascript code and avoid having Model and Controller classes.

    Thanks

    Tuesday, February 27, 2018 7:07 PM
  • User-1838255255 posted

    Hi gunderj,

    According to your description, you want to get the data through the AJAX call in html page, i think you could call controller action from the html page Ajax directly, it could return data. view doesn't need corresponding controller, model.  i have test it in my side, it works. 

    Like this:

    $.ajax({
      url: "/controllername/actionname",
      ......
    });

    if my understanding isn't right, please correct me! 

    Best Regards,

    Eric Du 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 1, 2018 8:49 AM