Search code examples
asp.net-mvcasp.net-coreasp.net-ajax

Ajax url Data result


Im trying to make a single view crud operation on ASP.NET MVC and having trouble getting data from ajax call and display it on a input field.

here the json actionresult enter image description here

heres the ajax call enter image description here

heres the data result enter image description here

how can i extract that data and display it to an input field

i try this but nothing workds enter image description here


Solution

  • According to your codes, you return the json return Json(new { data = objplan });, and your planid is not inside a list of objects.

    So you should could directly use the data.data.planid, it will return the right value, more details, you could refer to below codes:

    ASP.NET CORE:

        public IActionResult GetPlan(int id) {
            Plan objplan = new Plan { planID = 1, Price =1  };
            return Json(new { data = objplan });
        }
    
        public class Plan {
    
            public int planID { get; set; }
    
            public int Price { get; set; }
        }
    

    Ajax:

        function getplanbyid(planid) { 
        
            $.ajax({
    
                url: "/home/getplan?id=" + planid,
                type:"get",
                success: function (data) {
    
                    console.log(data.data.planID);
                }
            });
        }
    

    Result:

    enter image description here