skip to Main Content

I’m a newbie working with ajax. I have a problem while sending the data into ajax post.
The output of console.log(obj.Id) and console.log(oke) is 2. Then I tried to send it through data in ajax, but it end up 0 in the controller.

$(function () {
            $("body").on('click', '#btnEdit', function () {
                alert("clicked ok");
                $("#addRowModal").modal("hide");
                var obj = {};
                obj.Id = $(this).attr('data-id');
                oke = $(this).data("id");
                console.log(obj.Id)
                console.log(oke)
 
                $.ajax({
                    url: '@Url.Action("Details", "InvoicePPh")',
                    data: oke,
                    type: 'POST',
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function (response) {
                        alert("sukses");
                    },
                    error: function(response) { 
                        alert("error") 
                    }
                });
            });
        });

And my controller looks like this

[HttpPost]
        public JsonResult Details(int id)
        {
            var obj = dbContext.invoicePPhs.FirstOrDefault(s => s.Id == id);
            InvoicePPh pph = new InvoicePPh();
            pph2326.TaxForm = obj.TaxForm;              
            return Json(pph);
        }

I want the ‘2’ value that passes into my controller, how can I do that? Thank you for your help.

3

Answers


  1. Please change the data property in ajax part.

              $.ajax({
                    url: '@Url.Action("Details", "InvoicePPh")',
                    data: { 'id': oke },
                    type: 'POST',
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function (response) {
                        alert("sukses");
                    },
                    error: function(response) { 
                        alert("error") 
                    }
                });
    
    Login or Signup to reply.
  2. An alternative way to send your data your Controller method using Ajax would be to wrap your data in a JSON object and then send it to the server for processing. The server will be then deserialize your JSON object and you can access the required properties from that process:

    $(function () {
        $("body").on('click', '#btnEdit', function () {
            alert("clicked ok");
            $("#addRowModal").modal("hide");
            var obj = {};
            obj.Id = $(this).attr('data-id');
            oke = $(this).data("id");
            console.log(obj.Id)
            console.log(oke)
    
            var json = {
               oke: oke
            };
            
            $.ajax({
                url: '@Url.Action("Details", "InvoicePPh")',
                data: {'json': JSON.stringify(json)},
                type: 'POST',
                dataType: "json",
                success: function (response) {
                    alert("sukses");
                },
                error: function(response) { 
                    alert("error") 
                }
            });
        });
    });
    

    And your Controller method will be:

    using System.Web.Script.Serialization;
    
    [HttpPost]
    public JsonResult Details(string json)
    {
        var serializer = new JavaScriptSerializer();
        dynamic jsondata = serializer.Deserialize(json, typeof(object));
    
        //Get your variables here from AJAX call
        var id= Convert.Int32(jsondata["id"]);
        var obj = dbContext.invoicePPhs.FirstOrDefault(s => s.Id == id);
        InvoicePPh pph = new InvoicePPh();
        pph2326.TaxForm = obj.TaxForm;              
        return Json(pph);
    }
    
    Login or Signup to reply.
  3. If you just need id in your method parameter just change data in ajax to:

     contentType: "application/x-www-form-urlencoded",
     data: { 'id': oke },
    

    id is name of parameter from controller method.

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