For example I have the following model which I am passing a list of to the view:
public class ExampleViewModel
{
public int id { get; set; }
public string name { get; set; }
}
In my view I have the following:
@model List<ExampleViewModel>
<table>
<tr>
<th>Name</th>
<th>Action</th>
</tr>
@foreach (var x in Model)
{
<tr>
<td>
@Html.DisplayFor(m => x.name)
</td>
<td>
<input type="button" onclick="edit();" value="Edit">
</td>
</tr>
}
</table>
<script type="text/javascript">
function edit(x) {
window.location = "/Home/Edit?id=" + x
}
</script>
What I am having trouble with is passing x.id to the edit() function. I expected:
<input type="button" onclick="edit(@x.id);" value="Edit">
to work but it did not.
Thanks
Try this
<input type="button" onclick="edit(@x.id);" value="Edit">
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