首页 > 解决方案 > 通过链接单击将一个 int 值从视图传递到操作

问题描述

在我的视图中,我有一个关于表格的 for 循环。我能够显示结果。但是当我点击任何链接时。我得到一个空点异常。我发现这是因为循环结束了

 @model List<MyMainModel>
@using System.Collections;
@using System.Web;
@{

    ViewData["Title"] = "Display";
}

<h1>Display</h1>
<table border="1" cellpadding="20" style="table-layout:fixed">
    <tr>
        <th>
            UserName
        </th>
        <th>
            PAssword
        </th>

        <th>
            Email
        </th>
        <th colspan="2">
            Options
        </th>
       
    </tr>
    @{ for (int i = 0; i < Model.Count; i++)
        {

        <tr>
            <td>
                @Model.ElementAt(i).USERNAME
            </td>
            <td>
                @Model.ElementAt(i).PASSWORD
            </td>

            <td>
                @Model.ElementAt(i).EMAIL
            </td>
            <td >
               <a href="@Url.Action("RemoveRow", "MyMain")">Remove</a>
            </td>
            <td>
                <a href="@Url.Action("EditRow", "MyMain")">Edit</a>
            </td>
        </tr>
            }
        }
</table>
    

当我单击 Remove 时,我想将相应 Element 的 ID 传递回 RemoveRow Action 我该怎么做

标签: c#asp.net-mvc

解决方案


@Url.Action("RemoveRow", "MyMain", new { id = i })

推荐阅读