amala gopal das
amala gopal das

Reputation: 93

Mapping ViewModel to JS Object

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!

Upvotes: 3

Views: 2059

Answers (1)

Satpal
Satpal

Reputation: 133433

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

Upvotes: 5

Related Questions