Separating JavaScript in cshtml razor views

前端 未结 6 1880
半阙折子戏
半阙折子戏 2021-01-02 17:46

I\'m new to ASP.NET/MVC3 and I\'m trying to figure out how to separate my JavaScript (which contains C#) from the rest of the HTML.

If I put them into .JS files and

6条回答
  •  清歌不尽
    2021-01-02 18:18

    We convert our C# objects to Json with an extension on object and JavaScriptSerializer:

    public static string ToJson(this object item)
        {
            return new JavaScriptSerializer().Serialize(item);
        }
    

    Then we send set it on a variable (or in our case pass it to the constructor of the JavaScript Controller)

提交回复
热议问题