1

I have a situation where a when a web page is accessed a controller action runs which retrieves the data for that page based on a user selection. I am attempting to send the data back to the page as a JSON object, however, the data opens up as one large string in an HTML page. The controller action, in a nutshell, looks like the following snippet:

Public JsonResult MyMethod(string userSelection)
{

    string userData = (string) Data;
    return Json(userData, “text”, JsonRequestBehavior.AllowGet);
}

I first tried to use the JQuery $.getJson() method but I think this is wrong as I believe it issues another call to the action method for the data, which is not what I want to do. What I want is to access the JSON object in JavaScript code so I can use the property data to populate fields on the web page. The basic question is what must I do in my JavaScript to receive the JSON object when the page is first rendered? I apologize if I am missing something fundamental; this is my first try.

1
  • Please post the requested URL. Commented May 25, 2012 at 18:37

3 Answers 3

1

I still had no luck today but when I left work I came up with a strategy walking to my car. A user makes a selection from a page that presents a list prior to entering the page on which I cannot figure out how to work with JsonResult. Part of the problem is the user selection contains a link that calls the controller/action that returns the JsonResult which conflicts with using $.getJson() within the page where I want to work with JsonResult. So here is my strategy: When the user makes the selection that brings them to the (problematic) page, I will call a controller/action that strictly works with (ASP) ViewData, and use the ViewData to initially present that page. Once on the page, the user can change the selection -- I will handle this with a JavaScript event that uses a $.getJason() call to a different controller/action method that works with (ASP) JsonResult. After I try this strategy I shall post my results for whomever is interested.

Sign up to request clarification or add additional context in comments.

Comments

0

You want parseJSON not getJSON

http://api.jquery.com/jQuery.parseJSON/

Edit - Oh wait you are pointing your browser at the JsonResult as if it was an ActionResult? That is not going to work.

Render a proper view, and use getJSON to call the JsonResult action.

4 Comments

To return JSON from action, JSONResult will work same as ActionResult. JSonResult is inherited from ActionResult.
@Shyju Right, I think, but am not sure, the OP is saying his route is pointed at the JsonResult action, explaining the json string all over the page.
I think it is a problem somewhere in how he handle the data at client side.
@Shyju That's possible too. We need to see the route url to eliminate it as an option.
0

getJSON is what you are looking for. Call that on the DOM ready event which will executes once the DOM finishes loading.

$(function(){
 //This code will be executed on the DOM ready ( when the page is loaded)

  $.getJSON("YourControllerName/MyMethod?userSelection=someValue",function(data){

      alert(data.FirstName);     
      alert(data.AnotherPropertyName);    

   });
});

getJSON is a shorthand of jQuery ajax method with datatype set as json

Assuming your JSON data you are retuning is something like this

{
    "FirstName": "Scott",
    "AnotherPropertyName": "SomeValue"
}

To return data like above, change your Action method like this

public JsonResult MyMethod(string userSelection)
{
  var result=new { FirstName="Scott", AnotherPropertyName="Great"};
  return Json(result,JsonRequestBehavior.AllowGet);
}

2 Comments

You are using a Json overload I am unfamiliar with, the only overload I see that accepts a string as the second parameter sets the response MIME type... ?
@asawyer :Ah ! I copied a wrong thing from clipboard. Thanks for pointing. Corrected it.

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.