You must Sign In to post a response.
  • Category: ASP.Net MVC

    Crud operations using ajax call in mvc4

    Hi

    I am new to MVC , I want to perform crud operations(insert,update,delete) in mvc4 using the ajax call.
    Can any one provide me good example which includes all those for better understanding.
    And please describe the control flows between request and response ,,
    what is type: "PUT", type:'GET" ,
    how to use it,
    please provide the syntax and explain all those things that is require for ajax call and crud operation.
  • #766771
    Hi

    you can try this code for load data using json



    $(document).ready(function () {
    LoadDatas();
    });


    function LoadDatas() {
    $.ajax({
    url: '/home/GetContacts',
    type: 'GET',
    dataType: 'json',
    success: function (d) {
    if (d.length > 0) {
    var $data = $('<table></table>').addClass('table table-responsive table-striped');
    var header = "<thead><tr><th>Contact Person</th><th>Contact No</th></tr></thead>";
    $data.append(header);
    $.each(d, function (i, row) {
    var $row = $('<tr/>');
    $row.append($('<td/>').html(row.ContactPerson));
    $row.append($('<td/>').html(row.ContactNo));
    $data.append($row);
    });
    }
    },
    error: function () {
    alert('Error! Please try again.');
    }
    });
    }


    Controller Code


    public JsonResult GetContacts()
    {
    List<Contact> all = null;

    using (MyDBEntities dc = new MyDBEntities())
    {
    var contacts = (from a in dc.Contacts

    select new
    {
    a.ContactPerson,
    a.ContactNo
    });
    if (contacts != null)
    {
    all = new List<Contact>();
    foreach (var i in contacts)
    {
    Contact con=new Contact();
    con.ContactPerson ="AA";
    con.ContactNo = "123456";
    all.Add(con);
    }
    }
    }

    return new JsonResult { Data = all, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    }

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.


Sign In to post your comments