Lior Smila
Lior Smila

Reputation: 23

how to pass razor variable to js

I'm using MVC, I would like to pass a row index (razor variable) to a JS.

 @for (int i = 0; i < Model.Topics.Count(); i++)
                {
                    <tr id="row-@i"> 

                        <td class="IsReadOnly">@Html.TextBoxFor(model => model.Topics[i].NumberOfNoDifficulltySet, new { @readonly = "readonly", @class = "SubjectTB", @onchange = "CheckNoDiff(this);" })</td>

                    </tr>
                }

the js function is CheckNoDiff(this) - I would like to pass the @i parameter - that is the row index as an argument to the js. How do I do that? and how do I receive the parameter value on js?

Any help is most welcome - Regards, Lior.

Upvotes: 0

Views: 214

Answers (1)

Evidica
Evidica

Reputation: 1464

If you can use JQuery.data() then you can just add an attribute to your text box like so:

data-assigned-id="@i"

Then remove the parameter from the CheckNoDiff function and within the CheckNoDiff function you can access the value like so:

var id= $(this).data('assigned-id');

Upvotes: 2

Related Questions