简体   繁体   中英

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.

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM