C# 使用 Html.Raw 将 ASP.NET 模型序列化为 JSON 的 Razor 语法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17617263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:02:41  来源:igfitidea点击:

Razor syntax error serializing ASP.NET Model to JSON with Html.Raw

c#html.netasp.net-mvcrazor

提问by Tom Studee

This line is giving me a syntax error in Visual Studio 2012 (literally just 'Syntax Error'):

这一行在 Visual Studio 2012 中给了我一个语法错误(字面意思是“语法错误”):

var data = @Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model));

Modelin this case is the instance of @model MyApp.ViewModels.MyViewModeldeclared at the top of my cshtml.

Model在这种情况下是在@model MyApp.ViewModels.MyViewModel我的 cshtml 顶部声明的实例。

My model is serialized properly into the data var, and the application works correctly. Cosmetically it's just annoying to have the error permanently in my error list.

我的模型正确序列化到数据变量中,并且应用程序正常工作。从表面上看,将错误永久存在于我的错误列表中是很烦人的。

How should I modify the line so that the compiler is happy?

我应该如何修改该行以使编译器满意?

edit:

编辑:

As requested, more context. Here's the entire $(document).ready():

根据要求,提供更多上下文。这是整个$(document).ready()

<script type="text/javascript">

    $(document).ready(function () {

        $('#ReportDate').datepicker();
        $('#DispositionDate').datepicker();

        var data = @Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(Model));

        var vm = new NonconformingProductViewModel(data);
        ko.applyBindingsWithValidation(vm);

        // validate on page load so all reqd fields are highlighted.
        var valid = ko.validation.group(vm, {deep: true});
        valid.showAllMessages(true);

    }); // end document.ready

</script>

采纳答案by Andrei

Using function

使用功能

Implement a simple JavaScript setfunction that returns input argument:

实现一个set返回输入参数的简单 JavaScript函数:

function set(value){
    return value;
}

Use this function to assign Razor model value to a JavaScript variable:

使用此函数将 Razor 模型值分配给 JavaScript 变量:

var data = set(@Json.Encode(Model));


As an option you can use self-calling function:

作为一个选项,您可以使用自调用功能:

var data = function() { return set(@Json.Encode(Model)); }();

回答by haim770

Try to wrap it within a functionas follows:

尝试将其包装在 a 中function,如下所示:

var data = function() { return @Html.Raw(Json.Encode(Model)); }();

回答by Rich Bennema

Use JSON.Net, instead of either the JavaScriptSerializer or DataContractJsonSerializer, to avoid the nightmare that is JSON Dates:

使用JSON.Net,而不是 JavaScriptSerializer 或 DataContractJsonSerializer ,以避免JSON Dates 的噩梦

var data = function () { 
    return @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model)); }();

回答by Chad Kuehn

Even easier!! This will fix that little annoyance:

更轻松!!这将解决这个小烦恼:

var model = [@Html.Raw(Json.Encode(Model))][0];

Basically intellisense wants something around @Html.Raw. There is actually nothing wrong but we have to handle the intellisense shortcoming. Here we declare the result as the first index of a new array, then return the first index.

基本上智能感知想要一些东西@Html.Raw。实际上没有错,但我们必须处理智能感知的缺点。这里我们将结果声明为新数组的第一个索引,然后返回第一个索引。

FYI: If you want your model to reflect changes to the DOM then try the JSModelclass.

仅供参考:如果您希望您的模型反映对 DOM 的更改,请尝试使用JSModel类。

回答by sad_robot

You don't need to write any new javascript functions, just wrap the code into brackets

您不需要编写任何新的 javascript 函数,只需将代码括在括号中即可

var data = (@Html.Raw(Json.Encode(Model)));

works for me in Visual Studio 2015, not sure about VS2012

在 Visual Studio 2015 中对我有用,不确定 VS2012