问题
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