【问题标题】:Get row info from table using razor使用剃刀从表中获取行信息
【发布时间】:2014-06-16 03:04:33
【问题描述】:

我正在制作一个 ASP.NET MVC 应用程序,我目前有一个表:

|ID|first name|surname|
 1   bob        smith   Select <- (clickable)

我目前正在控制器中获取数据并将其传递给 ViewBag 中的视图,然后使用 razor 创建表。

<table>
    <thead>
        <tr>
            <th>Id</th>
            <th>First name</th>
            <th>surname</th>
        </tr>
    </thead>

    @foreach (var name in ViewBag.names)
    {
        <tr>
            <td>@name.Id</td>
            <td>@name.firstname</td>
            <td>@name.surname</td>
            <td>Select</td> <- shall make this clickable
        </tr>
    }
</table>

有没有办法从表中获取信息(当我单击选择按钮时)。理想情况下只是该行的 ID。

我也在尽量避免使用 javascript

目的是在我的控制器中调用 actionResult 方法,传入 id 作为参数

 public ActionResult Login(int id)
    {
// do stuff
}

【问题讨论】:

  • 这看起来像XY problem。您实际上要做什么,根据该行的 ID 在单击该行的按钮时执行一个操作?
  • 请出示您的代码
  • @alex 我想在我的控制器中调用一个 actionResult 方法,传入 id 作为参数
  • @Shoe 为你添加了一些代码
  • @BrianPeach 假设我正确理解了这个问题,我提出的解决方案应该符合标准

标签: c# asp.net sql asp.net-mvc razor


【解决方案1】:

您可以使用MVC HTML helper ActionLink(和各种其他帮助程序),特别是the one that accepts routeValues,创建您的可点击元素,并在那里使用ID。然后 MVC 将为您生成 URL。

asp.net 网站上的This tutorial 会在上下文中向您展示这些是如何工作的

@Html.ActionLink("Select", "Login", new { id=name.Id }) 

本教程的上一页解释了如何搭建和创建基本的 CRUD 操作。我强烈建议您通读文档。

【讨论】:

  • 您好,感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多