如何在asp.net-mvc中添加ActionLink?

时间:2019-05-11 16:30:23

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

但是我尝试将actionlink添加到我的代码中。我无法发表正确的看法。

table class="table table-bordered table-striped table">
    <thead>
        <tr>
            @foreach (System.Data.DataColumn col in Model.Columns)
            {
                <th>@col.Caption</th>
            }
        </tr>
    </thead>
    <tbody>
        @foreach (System.Data.DataRow row in Model.Rows)
        {
            <tr>
                @foreach (var cell in row.ItemArray)
                {
                    <td>@cell.ToString()</td>
                }
            </tr>
            <tr>
                @foreach (var cell in row.ItemArray)
                {

                    <td>@Html.ActionLink("Click for rent this car", "IntoSaleTable")</td>
                }
            </tr>
        }
        }
    </tbody>

由于上面的代码1

我的代码为单行生成了多个操作链接,但每行仅需要一个操作链接。

2 个答案:

答案 0 :(得分:0)

您的代码正在为每个单元格添加操作链接。您应该只渲染一次:

<table class="table table-bordered table-striped table">
    <thead>
        <tr>
            @foreach (System.Data.DataColumn col in Model.Columns)
            {
                <th>@col.Caption</th>
            }
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        @foreach (System.Data.DataRow row in Model.Rows)
        {
            <tr>
                @foreach (var cell in row.ItemArray)
                {
                    <td>@cell.ToString()</td>
                }

                <td>@Html.ActionLink("Click for rent this car", "IntoSaleTable")</td>
            </tr>
        }
    </tbody>
</table>

答案 1 :(得分:0)

根据您的评论,尝试以下操作:

<table class="table table-bordered table-striped table">
    <thead>
        <tr>
            @foreach (System.Data.DataColumn col in Model.Columns)
            {
                <th>@col.Caption</th>
            }
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (System.Data.DataRow row in Model.Rows)
        {
            <tr>
                @foreach (var cell in row.ItemArray)
                {
                    <td>@cell.ToString()</td>
                }
                <td>@Html.ActionLink("Click for rent this car", "IntoSaleTable")</td>  <!-- use row["Id"] to pass Id or somethig-->
            </tr>
        }
    </tbody>
</table>