So I'm trying to pass a parameter to a javascript function using the razor '@' notation (in an MVC-4 View) but I'm getting Syntax error: "Unterminated string constant"
Is there another way I should be writing this?
@foreach (var item in Model)
{
...
<input type="button" value="Assign" onclick="AssignButtonClicked('@item.ID')" />
}
I've used the same syntax as this answer https://stackoverflow.com/a/5179316/1662619
Edit:
It's just a syntax error, the functionality still works
If you can use JQuery .data()
Further it can be fetched using
try this
As alternative, you may use the other way to get
@item.ID
in your jQuery function. Just add hiden span with it and get it's value in your function.Say, you have list:
than in your script add:
But to my mind better approach for jQuery is as follows:
You tried with:
I added a semicolon at the end of javascript function call
AssignButtonClicked('@item.ID');
Try this,
Script
To avoid this kind of issue, you may build all your js function call in c# code section.
Instead of:
Use: