ASP.NET MVC Return Json Result?

后端 未结 1 561
天命终不由人
天命终不由人 2020-12-08 19:28

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

If I do it manually it works

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


        
相关标签:
1条回答
  • 2020-12-08 19:43

    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...

    0 讨论(0)
提交回复
热议问题