skip to Main Content

I am working in asp.net webforms and jquery ajax since many years and now want to implement a project in asp.ner core using same front end technology jQuery Ajax.

2

Answers


  1. jQuery implementation is same across multiple platforms. You can use similar code.

    Example of jquery ajax:

    $.ajax({
              type: 'POST',
              url: '/controller/Action'
              data: { parameter1 : 'value', parameter2 : 'value' },
              success: function (response) {
                           // code on successful completion
                       }
              error: function (response) {
                         // code on failure
                     }
    });
    
    Login or Signup to reply.
  2. You can apply inline data-ajax-* attributes as below:

    Include jquery libraries:

    @section Scripts {
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.unobtrusive-ajax.min.js"></script>
    }
    

    Apply data-ajax attributes to anchor element:

    <!-- ajax content will load inside this div -->
    <div id="data-div"></div>
    
    <a href="#" 
        data-ajax="true" 
        data-ajax-mode="replace" 
        data-ajax-update="#data-div" 
        data-ajax-loading="#loading"
        data-ajax-url="@Url.Page("Index", "ServerDate")">Run Ajax Request</a>
    
    <!-- spinning loading indicator -->
    <span id="loading" style="display:none;"> <i class="fas fa-spinner fa-spin"></i></span>
    

    and the backend method:

    public class IndexModel : PageModel
    {
        public void OnGet() { }
    
        public ContentResult OnGetServerDate()
        {
            return Content($"{DateTime.Now}");
        }
    }
    

    Same attributes can be applied to the forms as well.

    See demos here: http://demo.ziyad.info/en/AjaxRequest

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search