Reputation:
I have a problem about nested quotes.
<td style="width: 7%;padding-left:30px"><span class="btn btn-succes mainButton" data-toggle="modal" onclick="showStudents(@ViewBag.Acts[i].ActId, '@ViewBag.Acts[i].ActName','@ViewBag.Acts[i].Filter')" style="float: left; background: bisque"><i class="fa fa-sitemap"> Classes</i></span></td>
Above I have an code and it opens a modal. but it doesn't work. Because @ViewBag.Acts[i].ActName includes separation sign. so
Uncaught SyntaxError: missing ) after argument list
error occurs.
Upvotes: 0
Views: 156
Reputation: 239380
I think what you may be talking about is that the value of the variable itself contains a quote. For example, if the value of ViewBag.Acts[i].ActName
was Foo's
, then once Razer finished rendering the view you'd have something like:
onclick="showStudents(1, 'Foo's', ...
Obviously, that's a syntax error. The only thing that you can do in this situation is to escape the quotes:
onclick="showStudents(@ViewBag.Acts[i].ActId, '@ViewBag.Acts[i].ActName.Replace("'", "\\'")', ...
Which would result in:
onclick="showStudents(1, 'Foo\'s', ...
And you should be fine. You'll probably also need to do the same for double quotes, so you don't mess up the onclick
attribute. You just need to keep in mind that all Razor is doing is just dumping the value of the variable as-is among the rest of the HTML, JavaScript, whatever, so you have to think about what that would look like as just straight HTML.
Upvotes: 2
Reputation: 10824
You forgot single quote for first parameter of showStudents
:
...onclick="showStudents('@ViewBag.Acts[i].ActId'...
Upvotes: 0