Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If statement and Html.ActionLink in Razor MVC 3

@if (item.hasTypes.Value == true) { 
    Html.ActionLink(item.marketGroupName, "Index", new { id = item.marketGroupID });
}

I have this so that if the hasTypes is true, it will create an actionlink. But the above code does not work. It is showing empty in those columns.

like image 886
DevSharp Avatar asked Oct 16 '11 05:10

DevSharp


People also ask

What is HTML ActionLink in MVC?

Html. ActionLink creates a hyperlink on a view page and the user clicks it to navigate to a new URL. It does not link to a view directly, rather it links to a controller's action.

What is the use of HTML ActionLink?

ActionLink(HtmlHelper, String, String, Object, Object)Returns an anchor element (a element) for the specified link text, action, route values, and HTML attributes.

What is difference between HTML ActionLink and URL action?

There is a difference. Html. ActionLink generates an <a href=".."></a> tag whereas Url. Action returns only an url.


2 Answers

I think you forgot an @ which is used to output:

@if (item.hasTypes.Value) { 
    @Html.ActionLink(item.marketGroupName, "Index", new { id = item.marketGroupID });
}
like image 154
Darin Dimitrov Avatar answered Sep 30 '22 18:09

Darin Dimitrov


You need to actually render the link to the output. Your current code produces a link but doesn't actually do anything with it. Notice the extra @ below:

@if (item.hasTypes.Value == true) { 
    @Html.ActionLink(item.marketGroupName, "Index", new { id = item.marketGroupID });
}
like image 22
marcind Avatar answered Sep 30 '22 17:09

marcind