Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass a razor value to a jquery function, in an mvc view

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

like image 676
Jimmy Avatar asked Aug 19 '13 10:08

Jimmy


2 Answers

If you can use JQuery .data()

<input type="button" value="Assign" onclick="AssignButtonClicked(this)"         data-assigned-id="@item.ID" /> 

Further it can be fetched using

function AssignButtonClicked(elem){      var id= $(elem).data('assigned-id'); } 
like image 53
Satpal Avatar answered Sep 28 '22 03:09

Satpal


try this

 <input type="button" value="Assign"    onclick="@("AssignButtonClicked('"+item.ID+"')")"  /> 
like image 28
Amit Avatar answered Sep 28 '22 05:09

Amit