2

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 .?

4
  • Have u debugged in browser what result contains Commented Dec 4, 2013 at 9:20
  • Is it an ApiController or a regular Controller? Commented Dec 4, 2013 at 9:22
  • result contain a string System.generic..... bla bla Commented Dec 4, 2013 at 10:00
  • I'm confused. Seems like result is a list of ints, not a string. What am I missing? Commented Dec 4, 2013 at 10:02

1 Answer 1

10

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");
                }
        });
Sign up to request clarification or add additional context in comments.

4 Comments

But its only 15.42 hahahha ;)
@Nitinvarpe i add the above code but it shows alert from error function "Error:Unable to load data from server". i dnt know where is the problem
[InvalidOperationException]: This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet.
Check update add JsonRequestBehavior.AllowGet to returning json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.