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")'" />
newbie_86
  • 4,520
  • 17
  • 58
  • 89
  • 1
    **Where** do you get that error? In the browser, the compiler, or the IDE? – SLaks May 07 '13 at 13:53
  • 1
    Is it actually an error? Or just a squiggly line in the IDE? Does it compile and run ok? – Erik Funkenbusch May 07 '13 at 14:08
  • just a squiggly red line in the IDE, it runs fine – newbie_86 May 08 '13 at 08:17
  • 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. – atconway Jan 31 '14 at 14:44

2 Answers2

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")  
        + "'")" />
von v.
  • 16,868
  • 4
  • 60
  • 84
12

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

@Html.ActionLink("Cancel", "Index", "Home", null, new { @class = "btn btn-default" })
Marc
  • 3,683
  • 8
  • 34
  • 48
Ian
  • 121
  • 1
  • 2