繁体   English   中英

如何将 ID 从“tr onclick”Url.Action 方法传递给 controller

[英]How to pass ID from "tr onclick" Url.Action method to controller

我正在尝试创建一个包含行的表,该表在左列中显示带有 ID 的博客列表。 然后我想单击突出显示的行,它会将您重定向到具有与该ID对应的博客内容的视图。

所以,我的问题是如何在单击行时将Id (来自视图)传递给Display操作?

博客控制器.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();
}

索引.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>

只需在<tr>标记中对BlogId使用与 action 方法中使用的参数名称相同的参数名称:

 <tr onclick="location.href = '@(Url.Action("Display", "Blog", new { @BlogId = v.blogID }))'">

顺便说一句,在这里创建匿名类型时,不需要在BlogId之前使用@字符。 请参阅ASP.NET Web 使用 Razor 语法(C#)编程的介绍

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM