Javascript中,剃刀和转义字符。像撇号 [英] Javascript, Razor and Escape characters. Like apostrophe

查看:146
本文介绍了Javascript中,剃刀和转义字符。像撇号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的MVC3项目剃刀。而且我使用FullCalendar jQuery插件。所以,当我试图填补了数组它的作品不错。除了一件事。如果 s.Name 包含撇号它呈现像&放大器;#39; 这不是我想要的。我试图用不同的方法,如恩code和德code,甚至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.

下面是code片断:

<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: '@Html.Raw(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>
            }


  • Html.Raw 来跳过HTML转义。

  • &LT;文本方式&gt; 是多行输出更好

    • Html.Raw to skip the html escaping.
    • <text> is nicer for multiline output.
    • 这篇关于Javascript中,剃刀和转义字符。像撇号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆