剃刀语法错误序列化ASP.NET模型JSON与Html.Raw [英] Razor syntax error serializing ASP.NET Model to JSON with Html.Raw

查看:525
本文介绍了剃刀语法错误序列化ASP.NET模型JSON与Html.Raw的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此行​​是给我在Visual Studio 2012中有语法错误(字面意思只是'语法错误')

  VAR数据= @ Html.Raw(新System.Web.Script.Serialization.JavaScriptSerializer()序列化(型号));

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

我的模型正确序列化到数据var和应用程序正常工作。美容它只是恼人的永久有错误在我的错误列表。

我应该

如何修改行,这样编译器是幸福?

编辑:

根据要求,更多的上下文。这里就是整个 $(文件)。就绪()

 <脚本类型=文/ JavaScript的>    $(文件)。就绪(函数(){        $('#ReportDate')日期选择器()。
        $('#DispositionDate')日期选择器()。        VAR数据= @ Html.Raw(新System.Web.Script.Serialization.JavaScriptSerializer()序列化(型号));        VAR VM =新NonconformingProductViewModel(数据);
        ko.applyBindingsWithValidation(VM);        //验证页面加载,因此所有REQD字段高亮显示。
        VAR有效= ko.validation.group(VM,{深:真});
        valid.showAllMessages(真);    }); //结束的document.ready< / SCRIPT>


解决方案

使用功能

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

 函数集(值){
    返回值;
}

使用此功能剃刀模型值赋值给一个JavaScript变量:

  VAR数据=集(@ Json.En code(型号));


作为一个选项,您可以使用自调用函数:

  VAR数据=函数(){返回集(@ Json.En code(型号)); }();

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

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

Model in this case is the instance of @model MyApp.ViewModels.MyViewModel declared at the top of my 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():

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

解决方案

Using function

Implement a simple JavaScript set function that returns input argument:

function set(value){
    return value;
}

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

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


As an option you can use self-calling function:

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

这篇关于剃刀语法错误序列化ASP.NET模型JSON与Html.Raw的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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