ca9163d9
ca9163d9

Reputation: 29229

Generate links using values from the model

I have the following Mvc 4 razor code

@foreach (var item in Model.DealDetails) {
<tr>
    <td>@Html.DisplayFor(m => item.DetailId)</td>
    <td>@Html.DisplayFor(m => item.AppGuid)</td>
    ....
</tr>

I want to generate a link in another <td> column for each row. The link will look like

<a href="/Control/Action/{item.DetailId}?h={item.AppGuid}">
    Link text
</a>

The control and action are in the same project. Is there any help function to do it? I want the link not to be created when item.AppGuid is null.

Upvotes: 0

Views: 41

Answers (1)

AliRıza Adıyahşi
AliRıza Adıyahşi

Reputation: 15866

@foreach (var item in Model.DealDetails) {
<tr>
    <td>@Html.DisplayFor(m => item.DetailId)</td>
    <td>@Html.DisplayFor(m => item.AppGuid)</td>
    @if(item.AppGuid != null){
        <td>@Html.ActionLink("text", "action", "controller", new { id = item.DetailId , h = item.AppGuid}, null)</td>
    }
</tr>

Upvotes: 1

Related Questions