Outputting Json with the Razor view engine

后端 未结 3 1556
甜味超标
甜味超标 2021-01-02 08:34

I\'ve got a dictionary as part of my view model. What I\'m trying to do is cycle this object and output it as a json object. My reason for

3条回答
  •  一生所求
    2021-01-02 09:28

    Considering you are on mvc 3 you'll have access to JavaScriptSerializer. You should be able to do the following:

    JavaScriptSerializer serializer = new JavaScriptSerializer();
    string json = serializer.Serialize((object)yourDictionary);
    

    This will serialize your dictionary to json. You may want to do this in the controller before sending the ViewData to the view to render.

提交回复
热议问题