简体   繁体   English

在MVC C#中关闭html条件编译

[英]html conditional compilation is turned off in MVC C#

I wrote this function 我写了这个函数

 <script type="text/javascript">
    function saveDelivery() {
       alert("tttt")
       var model = @Html.Raw(Json.Encode(Model)); //errror
       $.ajax({

           type: 'POST',

           url: '@Url.Action("SaveDelivery", "Business")',
           contentType: 'application/json; charset=utf-8',
           data: JSON.serialize(model),
           success: function (result) {                                  
           },
           error: function (xhr, ajaxOptions, thrownError) {
               alert(xhr.status);
               alert(thrownError);
          }
      });

  }    

but there is error on 但是有错误

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

it says conditional compilation is turned off How to slove this? 它说conditional compilation is turned off如何解决这个问题?

You could use an Html Extension to output the script tags. 您可以使用Html扩展来输出脚本标记。 This can help help with intellisense problems in Visual Studio also. 这有助于解决Visual Studio中的智能感知问题。

public static class Extensions
{
    public static IHtmlString BeginScript(this HtmlHelper htmlHelper)
    {
       return new HtmlString("<script type=\"text/javascript\">");
    }

    public static IHtmlString EndScript(this HtmlHelper htmlHelper)
    {
        return new HtmlString("</script>");
    }
}

And then in your view: 然后在你看来:

@Html.BeginScript()

// JavaScript...

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

// More JavaScript...

@Html.EndScript()

NOTE: You'll need to add the namespace of your extension class to the <system.web.webPages.razor> element in web.config (the one in the views folder) 注意:您需要将扩展​​类的名称空间添加到web.config中的<system.web.webPages.razor>元素(views文件夹中的那个)

There are two options: 有两种选择:

  1. Do not care, you are sure about your code (it is warning) 不在乎,你确定你的代码(这是警告)
  2. Do something like this: var model = '@Html.Raw(Json.Encode(Model)) ', but probably you will have to change it to: var model = JSON.parse('@Html.Raw(Json.Encode(Model))') 做类似这样的事情: var model = '@Html.Raw(Json.Encode(Model)) ',但可能你必须把它改成: var model = JSON.parse('@Html.Raw(Json.Encode(Model))')

mvc 4 assembly reference missing for Json.Encode Json.Encode缺少mvc 4程序集引用

first add system.web.helper refrence 首先添加system.web.helper参考
then right click on this refrence property and change Copy Locaal=false to true and run your code with json.Encode 然后右键单击此refrence属性并将Copy Locaal = false更改为true并使用json.Encode运行代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM