Matt M
Matt M

Reputation: 3779

Why won't this link render?

I have an if/else statement in my details view like so:

 @if (Model.SomeProperty != null)
    {
        Html.RenderAction("MethodName", "ContollerName", new {id=stuff});
    }
    else
    { 
        <span>Is this showing?</span>
        Html.ActionLink("Link Text", "MethodName", "ContollerName", new { Id = something}, null);
    }                 

The span renders, so I know the else block was hit, but the ActionLink doesn't appear. However, if I move it out of the else block like this, it works:

@Html.ActionLink("Link Text", "MethodName", "ContollerName", new { Id = something}, null)

I'm guessing it's something wrong with my syntax, but I don't see it.

Upvotes: 0

Views: 108

Answers (3)

tpeczek
tpeczek

Reputation: 24125

It should be:

@if (Model.SomeProperty != null)
{
    Html.RenderAction("MethodName", "ContollerName", new {id=stuff});
}
else
{
    <span>Is this showing?</span>
    @Html.ActionLink("Link Text", "MethodName", "ContollerName", new { Id = something}, null);
}

The Html.RenderAction works directly with stream so it doesn't need it, but Html.ActionLink returns MvcHtmlString which needs to be outputed "in place".

Upvotes: 1

Roy Dictus
Roy Dictus

Reputation: 33149

The answer lies in the @! Just add it.

Upvotes: 1

SLaks
SLaks

Reputation: 888177

Html.ActionLink() returns a string (actual an IHtmlString) containing an <a> tag.

You aren't doing anything with that string.

You need to print the string to the page by writing @Html.ActionLink(...). (you can still do that in a code block)

Upvotes: 2

Related Questions