简体   繁体   中英

JSON object from ASP.NET controller to view

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.

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.

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.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM