Display control in Partial View conditionally

Asked

Viewed 1,688 times

2

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

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

For the control to be displayed on View the flame would be as follows:

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

So that the control nay was shown in View the flame would be as follows:

 @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>
}

Just do not trim the control, how to get around this problem?

  • I didn’t understand what the condition would be for Partial appear. I could explain this part better?

  • @Gypsy Morrison Mendez I will edit the question to be clearer.

  • Hey @Jota, all right? If you can put the condition so that the partial appears, it would help. Type, user permission, url, etc. .

  • @Randrade Beauty brother! It would have no condition, because this Partial view would be used at all views. Only in some cases the Clone control will appear in others not. So I wanted to put a parameter in the partial so that I could reuse the entire project. But I managed to solve it. Of course I accept suggestions to improve the solution found.

2 answers

2

The ideal is that the control of this Partial be done by View who calls this Partial:

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

This is due to the fact that its Partial will always depend on a ViewDataDictionary to be used. As the principle of Partials is the reuse of them, using the ViewDataDictionary must go against the general design standard of the application.

  • In case the partial would be the registry operation buttons (view, edit and delete) that would appear on all INDEX.CSHTNL. Only that in some cases besides the buttons listed above will be displayed the "clone" button. So to facilitate the reuse I thought it would be easier on the call of partial inside the view pass or not a parameter that defines whether or not the button should be displayed. If it is to be displayed it is only let ViewDataDictionarynull. Hence the test in partial. Now it goes against the design standard is a case to be analyzed. Thank you.

1


After a few tests I managed to solve the problem as follows.

In the View call to 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 }}) 

To nay flaunt:

 @Html.Partial("_smart_actions", item) 

In the Partial View I take the 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 who will use it.

  • This solution is not good because who should control the presentation or not of Partial is the View who calls the Partial. I’m gonna propose an answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.