首页 > 解决方案 > 如何使用 tr onlick 重定向到不同的 url

问题描述

我有这个 Razor 页面用户视图,其中包括表格中的学生列表

我想单击一行以打开一个 URL,该 URL 传递所选学生行的 ID。这是我到目前为止得到的

@foreach (var student in Model._User.Students)
        {
            <tr onclick="location.href = '@(Url.Action("Info", new { id = student.Id }))'">
                <td>Some basic info of student</td>
            </tr>
        }

我希望被重定向到 /Info/{id} 但得到 /Useview/{id}?action=Info

标签: asp.netasp.net-corerazor

解决方案


您可以使用Url.Page

<tr onclick="location.href = '@(Url.Page("Info", new { id = 1 }))'">

然后,如果您有另一个名为的页面Info,则可以获取参数:

@page "{id}"
@model RAZOR.Pages.InfoModel
@{
    ViewData["Title"] = "Info";
}

<p>The Id is @Model.Id</p>

信息.cshtml.cs:

public class InfoModel : PageModel
{
    public int Id { get; set; }
    public void OnGet(int id)
    {
        Id = id;
    }
}

推荐阅读