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