简体   繁体   中英

How to add actionlink in asp.net-mvc?

I tried to add actionlink to my code but. I am unable to produce proper view.

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>

As a result of above piece of code 1

My code produces multiple actionlinks for a single row, but I need just one actionlink per a single row.

Your code is adding the action link for each cell. You should render it only once:

<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>

Based on your comment, try this:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM