I am making an ASP.NET MVC app and I currently have a table:
|ID|first name|surname|
1 bob smith Select <- (clickable)
I am currently getting data in my controller and passing it to the view in a ViewBag then using razor to create the table.
<table>
<thead>
<tr>
<th>Id</th>
<th>First name</th>
<th>surname</th>
</tr>
</thead>
@foreach (var name in ViewBag.names)
{
<tr>
<td>@name.Id</td>
<td>@name.firstname</td>
<td>@name.surname</td>
<td>Select</td> <- shall make this clickable
</tr>
}
</table>
Is there a way to get information from the table (when I click the select button). Ideally just the Id for that row.
I am also trying to avoid javascript
The aim is to call an actionResult method in my controller passing in the id as a parameter
public ActionResult Login(int id)
{
// do stuff
}
You can create your clickable element using the MVC HTML helper ActionLink (and various other helpers), specifically the one that accepts routeValues, and use the ID there. MVC will then generate the URL for you.
This tutorial on the asp.net site shows you in context how these work
@Html.ActionLink("Select", "Login", new { id=name.Id })
The previous page in the tutorial explains how to scaffold and create basic CRUD operations. I'd highly suggest reading through the documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With