2013-04-06 35 views
1

我想將我的linq序列化爲JSON。我的問題是 Json result wrapped in pre tag - how to get value of it。答案不是我正在尋找的。 這裏是我的代碼 控制器在視圖中訪問控制器返回的JSON

return Json(regionBoudaries, JsonRequestBehavior.AllowGet); 

我看到我的JSON字符串寫入 的頁面現在我寫的東西像類似

查看

$(document).ready(function() {   
    initialize(); 
    process(a_variable); 
} 

如何設置a_variable從控制器返回的JSon的值。 請幫我一把。謝謝你在前進

+0

你能告訴我們你是如何調用你的控制器動作嗎?返回JSON?通常我們看到一個模式,其中一個控制器動作被調用來呈現視圖,然後該視圖中的一些客戶端JavaScript代碼調用另一個控制器動作來檢索和處理一些JSON。看起來你可能正在試圖做到這一步,而不是兩步? – 2013-04-07 15:58:31

回答

1

你可以使用一個視圖模型:

public class MyViewModel 
{ 
    public class SomeType RegionBoudaries { get; set; } 

    ... some other properties 
} 

,然後有服務這個視圖控制器動作填充視圖模型的財產:

public ActionResult SomeAction() 
{ 
    var model = new MyViewModel(); 
    model.RegionBoudaries = ... same stuff as in your other action 
    return View(model); 
} 

,然後在相應的強類型視圖:

@model MyViewModel 
... 
<script type="text/javascript"> 
    $(document).ready(function() {   
     var a_variable = @Html.Raw(Json.Encode(Model.RegionBoudaries)); 
     initialize(); 
     process(a_variable); 
    }); 
</script>