Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unterminated String Constant MVC4 Razor View Engine

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

enter image description here

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?

like image 428
Pawan Avatar asked Sep 04 '14 07:09

Pawan


1 Answers

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) )"
like image 168
ps2goat Avatar answered Nov 15 '22 11:11

ps2goat