Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

364
Views
Caracteres Javascript, Razor y Escape. como apóstrofe

Estoy usando Razor en mi proyecto MVC3. Y también estoy usando el complemento FullCalendar JQuery. Entonces, cuando intento llenar la matriz, funciona bien. Excepto una cosa. Si s.Name contiene un apóstrofe, se representa como ' eso no es lo que quiero. Traté de usar diferentes métodos como Encode y Decode e incluso MvcHtmlString.Create y el resultado es siempre el mismo.

Aquí está el fragmento de código:

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

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Escribiría tu foreach así:

 @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 para escapar de las comillas y el marcado html.
  • <text> es mejor para la salida de varias líneas.
over 4 years ago · Santiago Trujillo Report

0

Aquí está cómo hacerlo:

 title: '@Html.Raw(HttpUtility.JavaScriptStringEncode(s.Name))'
over 4 years ago · Santiago Trujillo Report

0

Prueba así:

 $(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 puede requerir algunas modificaciones para lograr el resultado deseado (en términos de nombres de propiedad), lo que me lleva al comentario habitual que hago sobre ViewBag cuando veo que alguien lo usa: usar ViewBag es una mala práctica y le recomendaría usar un vista fuertemente tipada con un modelo de vista.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!