映射视图模型到JS对象 [英] Mapping ViewModel to JS Object

查看:260
本文介绍了映射视图模型到JS对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能映射模型(服务器端)到JS对象吗?问题是,我无法从服务器端返回的JSON对象。

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:

View.cshtml:

@model TestModel

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

谢谢!

推荐答案

您可以使用的 Json.En code 方法的数据对象转换为字符串,它是在JavaScript对象符号(JSON)格式。

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))

这篇关于映射视图模型到JS对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆