0
我有一個這樣的視圖模型:從控制器傳遞字典爲Javascript
public class weightdata
{
...some properties
public string weights;
}
然後,我在控制器:
weightdata details = new weightdata();
Dictionary<string,float> dict = new Dictionary<string,float>();
//do something to fill the dictionary with values
var ser = new System.Web.Script.Serialization.JavaScriptSerializer();
details.weights = ser.Serialize(dict);
return View(details);
然後在視圖:
<script type="text/javascript">
var dict = @{Html.Raw(new JavaScriptSerializer().Deserialize<Dictionary<string,float>>(Model.Weights));}
</script>
但是頁面的渲染是: var dict =(它是空白的)
我怎樣才能得到這個值的字典到它可以被JavaScript使用的地方?