admin管理员组文章数量:1025244
I am using Razor in my MVC3 project. And also I'm using FullCalendar JQuery plugin. So when I'm trying to fill the array it works good. Except one thing. If s.Name contains apostrophe it renders like'
that's not what I want. I tried to use different methods like Encode and Decode and even MvcHtmlString.Create and result is always the same.
Here is the code snippet:
<head>
<script type='text/javascript'>
$(document).ready(function () {
$('#calendar').fullCalendar({
header: {
left: '',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
month: 5,
year: 2011,
editable: false,
events: [
@foreach (var s in ViewBag.Sessions)
{
@:{
@: title: '@s.Name',
@: start: new Date(@s.Starts.Year, @s.Starts.Month-1, @s.Starts.Day),
@: end: new Date(@s.Ends.Year, @s.Ends.Month-1, @s.Ends.Day)
@:},
}
]
});
});
</script>
I am using Razor in my MVC3 project. And also I'm using FullCalendar JQuery plugin. So when I'm trying to fill the array it works good. Except one thing. If s.Name contains apostrophe it renders like'
that's not what I want. I tried to use different methods like Encode and Decode and even MvcHtmlString.Create and result is always the same.
Here is the code snippet:
<head>
<script type='text/javascript'>
$(document).ready(function () {
$('#calendar').fullCalendar({
header: {
left: '',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
month: 5,
year: 2011,
editable: false,
events: [
@foreach (var s in ViewBag.Sessions)
{
@:{
@: title: '@s.Name',
@: start: new Date(@s.Starts.Year, @s.Starts.Month-1, @s.Starts.Day),
@: end: new Date(@s.Ends.Year, @s.Ends.Month-1, @s.Ends.Day)
@:},
}
]
});
});
</script>
Share
Improve this question
asked Mar 7, 2011 at 22:02
iLemmingiLemming
36.1k61 gold badges197 silver badges316 bronze badges
5 Answers
Reset to default 119I would write your foreach like this:
@foreach (var s in ViewBag.Sessions)
{
<text>
{
title: '@HttpUtility.JavaScriptStringEncode(s.Name)',
start: new Date(@s.Starts.Year, @s.Starts.Month-1, @s.Starts.Day),
end: new Date(@s.Ends.Year, @s.Ends.Month-1, @s.Ends.Day)
},
</text>
}
HttpUtility.JavaScriptStringEncode
to escape quotes and html markup.<text>
is nicer for multiline output.
Here is how to do it:
title: '@Html.Raw(HttpUtility.JavaScriptStringEncode(s.Name))'
Try like this:
$(function () {
$('#calendar').fullCalendar({
header: {
left: '',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
month: 5,
year: 2011,
editable: false,
events: @Html.Raw(new JavaScriptSerializer().Serialize(ViewBag.Sessions))
});
});
ViewBag.Sessions
might require some modifications to achieve the desired result (in terms of property names), which brings me to the usual remark I make about ViewBag
when I see someone using it: using ViewBag
is bad practice and I would recommend you using a strongly typed view with a view model.
HttpUtility.JavaScriptStringEncode
is not really required here.
Simply
'@Html.Raw(s.Name)'
is worked for me.
You said you already tried MvcHtmlString.Create, but for me, this seems to work correctly for me:
'Trying @MvcHtmlString.Create("Testing'`")'
.
Update:
I took your code '
, put it in browser, copied what showed in there, put it back in Visual Studio, like:
@MvcHtmlString.Create("'")
And it did work, I only got '
back, not '
.
.
Update 2:
This also works:
@{ViewBag.Symbol = "'";}
@MvcHtmlString.Create(ViewBag.Symbol)
I am using Razor in my MVC3 project. And also I'm using FullCalendar JQuery plugin. So when I'm trying to fill the array it works good. Except one thing. If s.Name contains apostrophe it renders like'
that's not what I want. I tried to use different methods like Encode and Decode and even MvcHtmlString.Create and result is always the same.
Here is the code snippet:
<head>
<script type='text/javascript'>
$(document).ready(function () {
$('#calendar').fullCalendar({
header: {
left: '',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
month: 5,
year: 2011,
editable: false,
events: [
@foreach (var s in ViewBag.Sessions)
{
@:{
@: title: '@s.Name',
@: start: new Date(@s.Starts.Year, @s.Starts.Month-1, @s.Starts.Day),
@: end: new Date(@s.Ends.Year, @s.Ends.Month-1, @s.Ends.Day)
@:},
}
]
});
});
</script>
I am using Razor in my MVC3 project. And also I'm using FullCalendar JQuery plugin. So when I'm trying to fill the array it works good. Except one thing. If s.Name contains apostrophe it renders like'
that's not what I want. I tried to use different methods like Encode and Decode and even MvcHtmlString.Create and result is always the same.
Here is the code snippet:
<head>
<script type='text/javascript'>
$(document).ready(function () {
$('#calendar').fullCalendar({
header: {
left: '',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
month: 5,
year: 2011,
editable: false,
events: [
@foreach (var s in ViewBag.Sessions)
{
@:{
@: title: '@s.Name',
@: start: new Date(@s.Starts.Year, @s.Starts.Month-1, @s.Starts.Day),
@: end: new Date(@s.Ends.Year, @s.Ends.Month-1, @s.Ends.Day)
@:},
}
]
});
});
</script>
Share
Improve this question
asked Mar 7, 2011 at 22:02
iLemmingiLemming
36.1k61 gold badges197 silver badges316 bronze badges
5 Answers
Reset to default 119I would write your foreach like this:
@foreach (var s in ViewBag.Sessions)
{
<text>
{
title: '@HttpUtility.JavaScriptStringEncode(s.Name)',
start: new Date(@s.Starts.Year, @s.Starts.Month-1, @s.Starts.Day),
end: new Date(@s.Ends.Year, @s.Ends.Month-1, @s.Ends.Day)
},
</text>
}
HttpUtility.JavaScriptStringEncode
to escape quotes and html markup.<text>
is nicer for multiline output.
Here is how to do it:
title: '@Html.Raw(HttpUtility.JavaScriptStringEncode(s.Name))'
Try like this:
$(function () {
$('#calendar').fullCalendar({
header: {
left: '',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
month: 5,
year: 2011,
editable: false,
events: @Html.Raw(new JavaScriptSerializer().Serialize(ViewBag.Sessions))
});
});
ViewBag.Sessions
might require some modifications to achieve the desired result (in terms of property names), which brings me to the usual remark I make about ViewBag
when I see someone using it: using ViewBag
is bad practice and I would recommend you using a strongly typed view with a view model.
HttpUtility.JavaScriptStringEncode
is not really required here.
Simply
'@Html.Raw(s.Name)'
is worked for me.
You said you already tried MvcHtmlString.Create, but for me, this seems to work correctly for me:
'Trying @MvcHtmlString.Create("Testing'`")'
.
Update:
I took your code '
, put it in browser, copied what showed in there, put it back in Visual Studio, like:
@MvcHtmlString.Create("'")
And it did work, I only got '
back, not '
.
.
Update 2:
This also works:
@{ViewBag.Symbol = "'";}
@MvcHtmlString.Create(ViewBag.Symbol)
本文标签: jQueryJavascriptRazor and Escape characters Like apostropheStack Overflow
版权声明:本文标题:jquery - Javascript, Razor and Escape characters. Like apostrophe - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1736930233a1439272.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论