Javascript, Razor and Escape characters. Like apostrophe(Javascript、Razor 和 Escape 字符.像撇号)
问题描述
我在我的 MVC3 项目中使用 Razor.而且我正在使用 FullCalendar JQuery 插件.因此,当我尝试填充数组时,效果很好.除了一件事.如果 s.Name 包含撇号,它会呈现为 ' 这不是我想要的.我尝试使用不同的方法,例如 Encode 和 Decode,甚至 MvcHtmlString.Create,结果始终相同.
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.
这里是代码片段:
<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>
推荐答案
我会这样写你的 foreach:
I 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转义引号和 html 标记.<text>更适合多行输出.HttpUtility.JavaScriptStringEncodeto escape quotes and html markup.<text>is nicer for multiline output.
这篇关于Javascript、Razor 和 Escape 字符.像撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Javascript、Razor 和 Escape 字符.像撇号
基础教程推荐
- Bokeh Div文本对齐 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
