How to parse ASP.NET MVC Action return Json format data

Last post 04-10-2009 4:46 AM by Augi. 5 replies.

Sort Posts:

  • How to parse ASP.NET MVC Action return Json format data

    04-10-2009, 3:52 AM
    • Member
      41 point Member
    • gracepig
    • Member since 11-02-2006, 4:35 AM
    • Posts 14

    Hi, folk

     I am having a weird problem now, when I use MVC Action to return a JSON string like following.

     

    public ActionResult SearchSegment(string keyword)
            {
                return Json(new {foo = "bar", baz = "Blech"});
            }
     

     

    But when I try to retrieve the return value using following code , I get the object not defined error. Can anyone help me out? Thanks.  

     function SearchCompleteHandler(data) {
            alert(data.foos);
           
        }

     

    Ho Ho Ha Hei
    Filed under:
  • Re: How to parse ASP.NET MVC Action return Json format data

    04-10-2009, 4:14 AM
    • Contributor
      6,375 point Contributor
    • Augi
    • Member since 10-18-2008, 5:46 AM
    • Czech Republic
    • Posts 1,101
    • TrustedFriends-MVPs

    C# code: foo
    JavaScript code: foos

    Don't forget to click "Mark as Answer" on the post that helped you.
  • Re: How to parse ASP.NET MVC Action return Json format data

    04-10-2009, 4:16 AM
    • Member
      41 point Member
    • gracepig
    • Member since 11-02-2006, 4:35 AM
    • Posts 14

    sorry, this is the typo, I am using following code. Still get the "undefinied" error

     

        function SearchCompleteHandler(data) {
            alert(data.foo);   
        }

    Ho Ho Ha Hei
  • Re: How to parse ASP.NET MVC Action return Json format data

    04-10-2009, 4:33 AM
    • Contributor
      6,375 point Contributor
    • Augi
    • Member since 10-18-2008, 5:46 AM
    • Czech Republic
    • Posts 1,101
    • TrustedFriends-MVPs

    I don't know how to you handle AJAX and Json in JavaScript - do you use jQuery or pure JavaScript?
    I would like to recommend to use jQuery getJSON function.
    These articles could help: http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=399 http://geekswithblogs.net/michelotti/archive/2008/06/28/mvc-json---jsonresult-and-jquery.aspx

    Don't forget to click "Mark as Answer" on the post that helped you.
  • Re: How to parse ASP.NET MVC Action return Json format data

    04-10-2009, 4:38 AM
    • Member
      41 point Member
    • gracepig
    • Member since 11-02-2006, 4:35 AM
    • Posts 14
    <% using (Ajax.BeginForm("SearchSegment", "SegmentCreate", new AjaxOptions {  OnSuccess = "SearchCompleteHandler" }))
       {%>

    Here is the code I used to issue the request.

    Ho Ho Ha Hei
  • Re: How to parse ASP.NET MVC Action return Json format data

    04-10-2009, 4:46 AM
    Answer
    • Contributor
      6,375 point Contributor
    • Augi
    • Member since 10-18-2008, 5:46 AM
    • Czech Republic
    • Posts 1,101
    • TrustedFriends-MVPs

    OK. OnSucces callback function is called with one parameter and type of this parameter is System.Mvc.AjaxContext. So you should call something like "data.get_data().foo" in your JavaScript code.

    Don't forget to click "Mark as Answer" on the post that helped you.
Page 1 of 1 (6 items)