ikey
ikey

Reputation: 331

How to get the id value of the selected row in a table created by ASP.Net Razor?

I've having a hard time getting the value of the id of the selected row in a table that was written using Razor.

I've tried this code:

@model IEnumerable<Payout.Models.user>


<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.username)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.email)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.username)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.email)
        </td>
        <td>
            <button type="button" class="btn btn-success submit" onclick="DeleteUser(item.userid)">Delete</button>
        </td>
    </tr>
}

</table>

But this code doesn't work and I just need to get the userid of the row where the button delete is clicked. The userid is not shown on the table but supplied in the model. Do you have any suggestions?

Upvotes: 0

Views: 1464

Answers (1)

Programnik
Programnik

Reputation: 1555

It's just missing the razor @ bang.

<td>
    <button type="button" class="btn btn-success submit" onclick="DeleteUser(@item.userid)">Delete</button>
</td>

Upvotes: 2

Related Questions