I'm trying to have a table with rows which displays a list of blogs with IDs in the left column. I want to then click a highlighted row and it will redirect you to a view with blog content corresponding to that ID
.
So, my question is how to do you pass the Id
(from view) when clicking on row to the Display
action?
BlogController.cs
public IActionResult Index()
{
var Result = from b in _db.blog
join aspu in _db.Users on b.userID equals aspu.Id
select new BlogViewModel {
blogID = b.blogID,
blogTitle = b.blogTitle,
firstName = aspu.firstName,
lastName = aspu.lastName, };
return View(Result);
}
public IActionResult Display(int blogid)
{
return View();
}
Index.cshtml:
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Blog Title</th>
<th scope="col">Date Created</th>
<th scope="col">Author</th>
<th scope="col"></th>
<th scope="col"></th>
</tr>
</thead>
<tbody id="sortable" style="cursor:pointer;">
@foreach (var v in Model)
{
<!-- below line when highlight row and click it, it passes the ID in that row to the display IActionResult-->
<tr onclick="location.href = '@(Url.Action("Display", "Blog", new { @Id = v.blogID }))'">
<td>@v.blogID</td>
<td>@v.blogTitle</td>
<td>@v.publishedDate</td>
<td>@(v.firstName + " " + v.lastName)</td>
<td>@Html.ActionLink("Edit", "Edit", "Post", new { @Id = v.blogID })</td>
<td>@Html.ActionLink("Delete", "Delete", "Post", new { @Id = v.blogID })</td>
@*<td><partial name="_DisplayEmployeePartial" model="v"></td>*@
</tr>
}
</tbody>
</table>
Just use the same parameter name in the <tr>
tag to BlogId
as it used in the action method:
<tr onclick="location.href = '@(Url.Action("Display", "Blog", new { @BlogId = v.blogID }))'">
By the way, you don't need to use @
character before the BlogId
when you are creating the anonymous type here. See Introduction to ASP.NET Web Programming Using the Razor Syntax (C#)
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.