I’m not able to get a clear answer on my problem: I want to have one partial view but have 3 distinct code blocks within the partial with similar (not exactly the same) content. So that whatever view page is loaded will determine which of the 3 code blocks is displayed in the partial view.
I did something similar back in MVC2, but it was on the controller level (controller determined which of the 3 code blocks was displayed). Now I have a need to go deeper and do it on the page level.
I understand that a bunch of if/else statements in a view is not the MVC convention. But honestly, that’s the only way I know how to do it. An alternative is to keep this if/else logic and create more than one controller and keep doing it the old way.
Can anyone point me in the right direction?
If your partial view looks something like this
You can design a ViewModel class which has some properties to determine which block of code to show
e.g.
Then, your partial view would look something like this
And the views calling the Partial View would use
Html.RenderPartial, and would pass a MyViewModel with the properties set, depending upon what they wanted to show. e.g.You should be able to use some variation of this.
As you will be aware, you should keep logic to a minimum in your Views, but the occasional if/then is OK.