1

I have one Partial View as follows

_MyNav.cshtml

<ul>
    <li>
        @Html.ActionLink("Link1", "Index", "Link",new { id="" }, null)
    </li>
    <li>
        @Html.ActionLink("Link2", "Index", " Link ",new { id="1" }, null)
    </li>
    <li>
        @Html.ActionLink("Link3", "Index", " Link ",new { id="2" }, null)
    </li>
</ul>

I included the partial view in two places in my main layout file. @Html.Partial("_MyNav"). One of the partial views needs to have all the links the other one needs to have two links.

Is there anyway that I would be able to hide one of the links in _MyNav by passing the parameter?

tereško
  • 58,060
  • 25
  • 98
  • 150
arlen
  • 1,065
  • 1
  • 16
  • 31
  • 2
    possible duplicate of [How to pass parameters to a partial view in ASP.NET MVC?](http://stackoverflow.com/questions/6549541/how-to-pass-parameters-to-a-partial-view-in-asp-net-mvc) – Brandon Oct 18 '12 at 20:50

1 Answers1

1

Make your partial strongly typed to a model (boolean in your case):

@model bool

<ul>
    <li>
        @Html.ActionLink("Link1", "Index", "Link",new { id="" }, null)
    </li>
    <li>
        @Html.ActionLink("Link2", "Index", " Link ",new { id="1" }, null)
    </li>
    @if (Model)
    {
        <li>
            @Html.ActionLink("Link3", "Index", " Link ",new { id="2" }, null)
        </li>
    }
</ul>

and then if you want to have 3 linkks:

@Html.Partial("_MyNav", true)

and if you want to have 2 links:

@Html.Partial("_MyNav", false)

Of course if you need to pass more complex information to the partial than just a boolean value you would define a view model and then make your partial strongly typed to this view model.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928