Development Tip

Javascript, Razor 및 Escape 문자.

yourdevel 2020. 12. 13. 11:18
반응형

Javascript, Razor 및 Escape 문자. 아포스트로피처럼


MVC3 프로젝트에서 Razor를 사용하고 있습니다. 또한 FullCalendar JQuery 플러그인을 사용하고 있습니다. 그래서 배열을 채우려 고 할 때 잘 작동합니다. 한 가지만 빼고. s.Name 에 아포스트로피가 포함되어 있으면 '내가 원하는 것이 아닌 것처럼 렌더링 됩니다. Encode 및 Decode 및 MvcHtmlString.Create와 같은 다른 방법을 사용하려고 시도했지만 결과는 항상 동일합니다.

다음은 코드 조각입니다.

<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를 작성합니다.

            @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 이스케이프를 건너 뜁니다.
  • <text> 여러 줄 출력에 더 좋습니다.

방법은 다음과 같습니다.

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

다음과 같이 시도하십시오.

$(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.


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 &#39;, 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 &#39;.

.

Update 2:

This also works:

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

HttpUtility.JavaScriptStringEncode is not really required here. Simply

 '@Html.Raw(s.Name)'

is worked for me.

참고URL : https://stackoverflow.com/questions/5225796/javascript-razor-and-escape-characters-like-apostrophe

반응형