[英]How to pass ID from "tr onclick" Url.Action method to controller
I'm trying to have a table with rows which displays a list of blogs with IDs in the left column.我正在尝试创建一个包含行的表,该表在左列中显示带有 ID 的博客列表。 I want to then click a highlighted row and it will redirect you to a view with blog content corresponding to that
ID
.然后我想单击突出显示的行,它会将您重定向到具有与该
ID
对应的博客内容的视图。
So, my question is how to do you pass the Id
(from view) when clicking on row to the Display
action?所以,我的问题是如何在单击行时将
Id
(来自视图)传递给Display
操作?
BlogController.cs博客控制器.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:索引.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>
标记中对BlogId
使用与 action 方法中使用的参数名称相同的参数名称:
<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.顺便说一句,在这里创建匿名类型时,不需要在
BlogId
之前使用@
字符。 See Introduction to ASP.NET Web Programming Using the Razor Syntax (C#)请参阅ASP.NET Web 使用 Razor 语法(C#)编程的介绍
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.