Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7565155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:04:46+00:00 2026-05-30T14:04:46+00:00

I’m not able to get a clear answer on my problem: I want to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T14:04:47+00:00Added an answer on May 30, 2026 at 2:04 pm

    If your partial view looks something like this

    <div>...Generic stuff used all the time...</div>
    ...
    <div>...special block used only in some circumstances...</div>
    ...
    <div>...another special block used only in some other circumstance...</div>
    ...etc...
    

    You can design a ViewModel class which has some properties to determine which block of code to show

    e.g.

    public class MyViewModel 
    {
       public bool ShowBlockOne { get; set; }
       public bool ShowBlockTwo { get; set; }
    }
    

    Then, your partial view would look something like this

    @model MyViewModel 
    <div>...Generic stuff used all the time...</div>
    ...
    @if (Model.ShowBlockOne) {
        <div>...special block used only in some circumstances...</div>
    }
    ...
    @if (Model.ShowBlockTwo) {
        <div>...another special block used only in some other circumstance...</div>
    }
    ...etc...
    

    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.

    @{ Html.RenderPartial("MyPartial", new MyViewModel { ShowBlockOne = true } ); }
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I want to construct a data frame in an Rcpp function, but when I
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.