Razor syntax error serializing ASP.NET Model to JSON with Html.Raw

后端 未结 5 439
臣服心动
臣服心动 2020-12-09 03:09

This line is giving me a syntax error in Visual Studio 2012 (literally just \'Syntax Error\'):

var data = @Html.Raw(new System.Web.Script.Serialization.JavaS         


        
5条回答
  •  醉梦人生
    2020-12-09 04:05

    Even easier!! This will fix that little annoyance:

    var model = [@Html.Raw(Json.Encode(Model))][0];
    

    Basically intellisense wants something around @Html.Raw. There is actually nothing wrong but we have to handle the intellisense shortcoming. Here we declare the result as the first index of a new array, then return the first index.

    FYI: If you want your model to reflect changes to the DOM then try the JSModel class.

提交回复
热议问题