I am working on MVC4
Application (Razor engine). When ever I try to call a js function from a button, it says error :-
Unterminated string constant
However it does not give any compile time
or runtime
error and works fine.
But on view there it clearly says about above mentioned error:
why it is so?
The Razor parser is picky, and in some cases it doesn't correctly parse or color-code the syntax. Your code will work.
To fix the issue, you can format the code in a way that the parser will know exactly how to figure this out. Use parentheses around all Razor server-side code references:
<input type="button" id="btntest" value="Details"
onclick="ABC('@(Item.TourApplicationID)', '@(Item.TourID)')" />
Edit: above may have only been working in VS 2013, with or without an update. (or not at all- I just remember it working.
Below should work, taken from MVC3 unterminated string constant syntax error:
<input type="button" id="btntest" value="Details"
onclick="@("ABC('" & Item.TourApplicationID & "', '" & Item.TourID & "')")" />
(I think that's correct. Hard to tell without the color-coding.)
I prefer string.format, so that onclick would look more like:
onclick="@(String.Format("ABC('{0}', '{1}')", Item.TourApplicationID, Item.TourID) )"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With