Mapping ViewModel to JS Object

痞子三分冷 提交于 2020-01-04 03:15:23

问题


Is there possible to map a model (server side) into JS object? The problem is that I can not return a json object from the server side.

View.cshtml:

@model TestModel

<script type="text/javascript">
    jQuery(function ($) {
        var jsObject = {
            Property1: @Model.Property1,
            Property2: @Model.Property2,
            Property3: @Model.Property3,
            ...
        };
    });
</script>

Thanks!


回答1:


You can use Json.Encode Method converts a data object to a string that is in the JavaScript Object Notation (JSON) format.

var jsObject = @Html.Raw(Json.Encode(Model))


来源:https://stackoverflow.com/questions/24493588/mapping-viewmodel-to-js-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!