2011-01-05 14 views
10

Witam Próbuję wysłać ciąg do widoku, który wygląda jak json.C# do json nie renderowanie poprawnie w widoku

Im wysyłając listę miejsc:

class Place 
     { 
      public string title { get; set; } 
      public string description { get; set; } 
      public double latitude { get; set; } 
      public double longitude { get; set; } 
     } 

List<Place> placeList = new List<Place>(); 
//add places to PlaceList 

//Then i do this 
System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 
      string sJSON = oSerializer.Serialize(placeList); 
      ViewBag.Places = sJSON; 

W widoku jego wyjście renderingu tak jakby:

[{&quot;title&quot;:&quot;sdf sdfsd sdf sd f&quot;,&quot;description&quot;:&quot;sdf sdf sd fsd sd sdf sdf dssd sdf sd s&quot;,&quot;latitude&quot;:53.740259851464685,&quot;longitude&quot;:-2.4602634343627927}, 

Jak mogę zmusić go do renderowania jako normalne json w widoku? minus &quot; itp?

Odpowiedz

20

W swoim komentarzu poniżej ciebie, że twój pogląd jest za pomocą @ViewBag.Places

Używasz Razor? Jeśli tak, to składnia @ robi to samo co <%: - koduje zawartość.

pomocą interfejsu IHtmlString, aby tego uniknąć, więc albo:

ViewBag.Places = new HtmlString(sJSON); 

Albo

@HtmlString(ViewBag.Places) 
1

próbowałeś?

string sJSON = HttpServerUtility.HmltDecode(oSerializer.Serialize(placeList)); 
+0

też myślę, że to dziwne, że oSerializer.Serialize powrót html zakodowany ciąg. Czy jesteś pewien, że renderowanie w twoim widoku nie ma żadnego wstępnego przygotowania? –

+0

Próbowałem: HttpUtility.HtmlDecode (oSerializer.Serialize (placeList)); - ten sam wynik: – raklos

+0

w moim widoku mam: "places": @ ViewBag.Places – raklos

Powiązane problemy