View control in Partial View conditionally

2

I need a Partial View control to be displayed according to a condition. For some views a button would appear for others not and this would be set via a parameter in the Partial View call.

After searching I saw that I can use ViewDataDictionary , but when trying to implement I did not succeed.

For the control to be displayed in View the call would be as follows:

 @Html.Partial("_smart_actions", item, new ViewDataDictionary {{ "MenuClone", true }}) 

For the control to be displayed in View the call would look like this:

 @Html.Partial("_smart_actions", item) 

Partial View :

<a href="@Url.Action("Details", new { id = Model.Id })" class="link-mutted" title="Visualizar">
    <span class="glyphicon glyphicon-share" aria-hidden="true"></span>
</a>
|
<a href="@Url.Action("Edit", new { id = Model.Id })" class="link-mutted" title="Editar">
    <span class="glyphicon glyphicon-edit" aria-hidden="true"></span>
</a>
|
<a href="@Url.Action("Delete", new { id = Model.Id })" class="link-mutted" title="Excluir">
    <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
</a>
@if (ViewDataDictionary == true)
{
<a href="@Url.Action("DuplicarDados")" class="link-mutted" title="Duplicar">
    <span class="glyphicon glyphicon-duplicate" aria-hidden="true"></span>
</a>
}

It just does not seem to control, how to get around this problem?

    
asked by anonymous 09.04.2015 / 04:04

2 answers

1

After some tests I solved the problem as follows.

In View I call Partial View with a parameter that defines whether or not the control will be displayed.

To display:

 @Html.Partial("_smart_actions", item, new ViewDataDictionary {{ "MenuClone", true }}) 

For no display:

 @Html.Partial("_smart_actions", item) 

In Partial View I test like this:

<a href="@Url.Action("Details", new { id = Model.Id })" class="link-mutted" title="Visualizar">
    <span class="glyphicon glyphicon-share" aria-hidden="true"></span>
</a>
|
<a href="@Url.Action("Edit", new { id = Model.Id })" class="link-mutted" title="Editar">
    <span class="glyphicon glyphicon-edit" aria-hidden="true"></span>
</a>
|
<a href="@Url.Action("Delete", new { id = Model.Id })" class="link-mutted" title="Excluir">
    <span class="glyphicon glyphicon-trash" aria-hidden="true"></span>
</a>
@if (ViewData["MenuClone"] != null && ViewData["MenuClone"].Equals(true))
{
    <text>
        |
        <a href="@Url.Action("Clonar")" class="link-mutted" title="Clonar">
            <span class="glyphicon glyphicon-duplicate" aria-hidden="true"></span>
        </a>
    </text>
}

This allows me to have a single Partial View that can be reused throughout the project and displays its content according to the condition passed by View that will use it .

    
09.04.2015 / 17:59
2

@if (condicao) {
    @Html.Partial("_smart_actions", item)
}

This is due to the fact that your Partial will always depend on ViewDataDictionary to be used. Since the principle of Partials is their reuse, using ViewDataDictionary necessarily goes against the general design pattern of the application.

    
09.04.2015 / 18:17