最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - html conditional compilation is turned off in MVC C# - Stack Overflow

programmeradmin4浏览0评论

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 pilation is turned off How to slove this?

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 pilation is turned off How to slove this?

Share Improve this question edited Apr 11, 2013 at 7:35 Rui Jarimba 18.2k11 gold badges64 silver badges98 bronze badges asked Apr 11, 2013 at 7:24 aruniaruni 2,75210 gold badges46 silver badges68 bronze badges 1
  • 2 try this may be it will work var model = "@Html.Raw(Json.Encode(Model))"; – Amit Commented Apr 11, 2013 at 7:25
Add a ment  | 

3 Answers 3

Reset to default 3

You could use an Html Extension to output the script tags. This can help help with intellisense problems in Visual Studio also.

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)

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

mvc 4 assembly reference missing for Json.Encode

first add system.web.helper refrence
then right click on this refrence property and change Copy Locaal=false to true and run your code with json.Encode

发布评论

评论列表(0)

  1. 暂无评论