Home > DeveloperSection > Forums > Return List to ajax mvc3
jacob rasel
jacob rasel

Total Post:88

Points:616
Posted on    December-03-2014 10:17 PM

 ASP.Net AJAX  ASP.NET MVC  Mvc3  ListView 
Ratings:


 1 Reply(s)
 931  View(s)
Rate this:

I am working on MVC3 and following is my controller

 

  public List<int> ddlTransType_Change(int DocID)

        {

 

            return UserDocumentServive.getSelectedUsers(DocID);

 

        }

My Ajax

 

  $.ajax({

                type: 'GET',

                url: "/MIS.MVC/" + "DocumentApproval/ddlTransType_Change",

                data: {

                    'DocID': $("#ddlTransType").val().trim()

                },

                success: function (result) {

                    alert(result.value)

},

error: function (e) {

                    alert("Error:Unable to load data from server");

                }

            });

Controller returns a list of int values i.e {1,74,23,1} and I want to show them in alert. any idea how to do it .?



Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Posted on    December-04-2014 7:31 AM

If you are using MVC3, its better to return json data back to your ajax success call

 

public ActionResult ddlTransType_Change(int DocID)

    {

         List<int> list = UserDocumentServive.getSelectedUsers(DocID);;

        return Json(new

        {

            list = list

        },JsonRequestBehavior.AllowGet);

    }

Then your ajax call changes to

 

 $.ajax({

                type: 'GET',

                url: "/MIS.MVC/" + "DocumentApproval/ddlTransType_Change",

                data: {'DocID': $("#ddlTransType").val().trim()},

                dataType: 'json',

                success: function (result) {

                                           var list=result.list;

              $.each( list, function( index, value )

                                         {

                                            alert(value);

                                           });

                                             },

error: function (e) {

                    alert("Error:Unable to load data from server");

                }

        });


Don't want to miss updates? Please click the below button!

Follow MindStick