Javascript、Razor 和转义字符。像撇号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5225796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:14:55  来源:igfitidea点击:

Javascript, Razor and Escape characters. Like apostrophe

javascriptjqueryjquery-pluginsasp.net-mvc-3razor

提问by iLemming

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.Namecontains 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.

我在我的 MVC3 项目中使用 Razor。而且我正在使用 FullCalendar JQuery 插件。因此,当我尝试填充数组时,它运行良好。除了一件事。如果s.Name包含撇号,它会呈现出'这不是我想要的。我尝试使用不同的方法,如编码和解码,甚至 MvcHtmlString.Create 和结果总是相同的。

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>

回答by GvS

I would write your foreach like this:

我会这样写你的 foreach:

            @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.Rawto skip the html escaping.
  • <text>is nicer for multiline output.
  • Html.Raw跳过 html 转义。
  • <text>对于多行输出更好。

回答by Fabrice

Here is how to do it:

这是如何做到的:

title: '@Html.Raw(HttpUtility.JavaScriptStringEncode(s.Name))'

回答by Darin Dimitrov

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.Sessionsmight require some modifications to achieve the desired result (in terms of property names), which brings me to the usual remark I make about ViewBagwhen I see someone using it: using ViewBagis bad practice and I would recommend you using a strongly typed view with a view model.

ViewBag.Sessions可能需要进行一些修改才能获得所需的结果(在属性名称方面),这让我想起了ViewBag当我看到有人使用它时的通常评论:使用ViewBag是不好的做法,我建议您使用带有查看模型。

回答by Meligy

You said you already tried MvcHtmlString.Create, but for me, this seems to work correctly for me:

你说你已经尝试过 MvcHtmlString.Create,但对我来说,这对我来说似乎工作正常:

'Trying @MvcHtmlString.Create("Testing'`")'

.

.

Update:

更新:

I took your code &#39;, put it in browser, copied what showed in there, put it back in Visual Studio, like:

我拿了你的代码&#39;,把它放在浏览器中,复制那里显示的内容,把它放回 Visual Studio,比如:

@MvcHtmlString.Create("'")

And it did work, I only got 'back, not &#39;.

它确实有效,我只'回来了,没有&#39;

.

.

Update 2:

更新 2:

This also works:

这也有效:

@{ViewBag.Symbol = "'";}
@MvcHtmlString.Create(ViewBag.Symbol)

回答by Vijai Sebastian

HttpUtility.JavaScriptStringEncodeis not really required here. Simply

HttpUtility.JavaScriptStringEncode这里并不真正需要。简单地

 '@Html.Raw(s.Name)'

is worked for me.

对我有用。