【问题标题】:How to redirect to a different url using tr onlick如何使用 tr onclick 重定向到不同的 url
【发布时间】:2019-06-01 13:35:50
【问题描述】:

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

我想单击一行以打开一个 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.net asp.net-core razor


    【解决方案1】:

    你可以使用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>
    

    Info.cshtml.cs:

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

    【讨论】:

      猜你喜欢
      • 2017-07-15
      • 2013-03-04
      • 1970-01-01
      • 2014-01-19
      • 2017-11-30
      • 1970-01-01
      • 2016-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多