Visual Studio "Conditiional Compilation Disabled" Error while viewing while transferring JSON object

I am passing a JSON object to Javascript through a ViewBag with the following code in my opinion:

var jsonResultData = @Html.Raw(ViewBag.JsonResultData);

This approach works fine, but VisualStudio continues to issue a warning "Conditional compilation is disabled." It seems VS wants quotes around @ Html.Raw (ViewBag.JsonResultData); If I add quotes, jQuery sees the variable as a string, not JSON data.

Is my approach wrong? Is there any other way that I should approach this? If not, can I turn off VS warning? An unfortunate side effect of the warning is that I cannot format my code using CTRL KD. A.

+3
source share
1 answer

ViewBag? , JSON, ? - :

public ActionResult Foo()
{
    var model = ...
    ViewBag.JsonResultData = new JavaScriptSerializer().Serialize(model);
    return View(model);
}

. :

public ActionResult Foo()
{
    var model = ...
    return View(model);
}

:

<script type="text/javascript">
    var jsonResultData = @Html.Raw(Json.Encode(Model));
</script>

, , Razor Intellisense . , javascript. , ASP.NET MVC. . , , Visual Studio, , , .

+9

All Articles