admin管理员组文章数量:1399287
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?
- 2 try this may be it will work var model = "@Html.Raw(Json.Encode(Model))"; – Amit Commented Apr 11, 2013 at 7:25
3 Answers
Reset to default 3You 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:
- Do not care, you are sure about your code (it is warning)
- 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
本文标签: javascripthtml conditional compilation is turned off in MVC CStack Overflow
版权声明:本文标题:javascript - html conditional compilation is turned off in MVC C# - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744134623a2592339.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论