ASP.NET MVC - Pass Json String to View using ViewData

前端 未结 1 925
执笔经年
执笔经年 2020-12-30 05:17

I\'m trying to pass Json to my View using ViewData

Controller

ViewData(\"JsonRegionList\") = Json(RegionService.GetActiveRegions())
         


        
相关标签:
1条回答
  • 2020-12-30 05:44

    The Json() controller method returns a JsonResult, which isn't the same as a JSON string. The JsonResult holds data, but the data is actually written directly to the response when the View Engine calls JsonResult.ExecuteResult(). That's all probably more information than you want there - the point is that calling Json() in a controller won't give you a string of JSON.

    If you just want to turn your data into a JSON string, you can use the JavaScriptSerializer, which is what the Json() method uses internally:

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    ViewData["JsonRegionList"] = serializer.Serialize(jsonRegionList); 
    
    0 讨论(0)
提交回复
热议问题