views:

34

answers:

1

Hi, How can i pass the json output from controller action to its view ? As I tried to send before, My code is :

public ActionResult Index() { Guid Id = new Guid("66083eec-7965-4f3b-adcf-218febbbceb3"); List officersTasks = tasks_to_officer_management.GetTasksToOfficers(Id); return Json(officersTasks) }

it is asking for JsonRequestBehavior.AllowJson like parameter. I know it is new in asp.net mvc 2 but as redirect to view there is nothis happens but asking for download the json output file. I want to work with returned data in my Jquery . but something going wrong there. and if I removed the parameter then it is showing error :
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.


How to avoid this and get json data at view ?

+6  A: 

Here is an example of what you are trying to do. First in your view you call $.getJSON to grab the JSON data from the action:

 $.getJSON('/Data/StockQuote', function(data) {
    if (data.success) {
       ShowStockQuote(data);                         
    }
 });

Then your action will look like this:

   public JsonResult GetStockQuote()
   {                 
       JsonResult result = new JsonResult()
       {
          Data = new { 
                 lastTradePrice = 50,
                 lastUpdated = "10/1/2010",
                 expirationDate = "10/2/2010",
                 success = true
           },
           JsonRequestBehavior = JsonRequestBehavior.AllowGet
        };

       return result;
   }

Once the JSON data is returned from your action to the $.getJSON you can use data to access all the values off of the JSON object. So data.success will give you the success and so forth.

amurra
Thanks for reply , but what is StockQuote and ShowStockQuote(data) in this code ? I think action is name is GetStockQuote(). what i have to consider there >
Lalit
ok but can i keep my action return type as Json result? instead of ActionResult ? I tried it it takes return type as like above , but still asking for download . what have to do ?
Lalit
helllloooooooooo is anybody there ? please solve this. or please tell me is there any option rather than json ?
Lalit
@Lalit - ShowStockquote(data) is a javaScript function and StockQuote is part of the URL that would map to the StockQuote action. JSONResult inherits from ActionResult so you can leave it. Post your HTML if you would like more help.
amurra