40

I'm trying to return a JSON result (array);

If I do it manually it works

 resources:[
{
 name: 'Resource 1',
 id: 1,
 color:'red'
},{
 name: 'Resource 2',
 id: 2
}],

but I'm having issues rendering by passing it in:

On the view:

 resources:@Model.Resources

Which on the controller

public ActionResult Index()
 {
...
var model = new Display();
model.Resources = GetResources();
}
 public JsonResult GetResources()
 {
 var model = new Models.ScheduledResource()
 {
 id = "1",
 name = "Resource"
 };
 return new JsonResult() { Data = model, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
 }

on the model

public JsonResult Resources { get; set; }

But looking at whats rendered in HTML:

resources:System.Web.Mvc.JsonResult

Any ideas where I'm going wrong?

Heretic Monkey
12.2k7 gold badges62 silver badges133 bronze badges
asked May 30, 2013 at 12:38
2
  • possible duplicate of JSONResult to String Commented May 30, 2013 at 12:45
  • It should be : return Json(new { Data = model } , JsonRequestBehavior = JsonRequestBehavior.AllowGet); Commented May 30, 2013 at 13:05

1 Answer 1

84

It should be :

public async Task<ActionResult> GetSomeJsonData()
{
 var model = // ... get data or build model etc.
 return Json(new { Data = model }, JsonRequestBehavior.AllowGet); 
}

or more simply:

return Json(model, JsonRequestBehavior.AllowGet); 

I did notice that you are calling GetResources() from another ActionResult which wont work. If you are looking to get JSON back, you should be calling GetResources() from ajax directly...

answered May 30, 2013 at 13:12
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.