MVC3 unterminated string constant syntax error
Asked Answered
Q

2

6

I have the following code which gives me a syntax error - unterminated string constant.I've matched up the quotes can't seem to spot an issue. Any ideas? It works fine, the syntax error is just annoying.

  <input type="button" class="my-button" value="" name="back" onclick="location.href='@Url.Action(Model.Back.Step.ToString(), "MyController")'" />
Quarterback answered 7/5, 2013 at 13:50 Comment(4)
Where do you get that error? In the browser, the compiler, or the IDE?Bhili
Is it actually an error? Or just a squiggly line in the IDE? Does it compile and run ok?Angy
just a squiggly red line in the IDE, it runs fineQuarterback
For me it was just a squiggly line in the IDE view, and technically worked. However the rendered HTML was all messed up at runtime. It looked like 2 buttons sandwiched in some text. I had to use the solution from @vonv. with the concatenation.Subservient
T
20

You can rewrite it like this:

<input type="button" class="my-button" value="" name="back" 
     onclick="@("location.href='" 
        + Url.Action(Model.Back.Step.ToString(), "MyController")  
        + "'")" />
Turmel answered 7/5, 2013 at 13:54 Comment(0)
B
12

Use an actionlink instead. This one creates a nice bootstrap button:

@Html.ActionLink("Cancel", "Index", "Home", null, new { @class = "btn btn-default" })
Ba answered 31/3, 2015 at 20:17 Comment(1)
Clean Solution. Thanks for the postClippard

© 2022 - 2024 — McMap. All rights reserved.