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

  • SEARCH
  • Home
  • 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 4001450
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:54:23+00:00 2026-05-20T07:54:23+00:00

Just started converting to MVC from classic ASP and wondering about best practice for

  • 0

Just started converting to MVC from classic ASP and wondering about best practice for if-statements that is used to decide if a HTML-element should be visible or deciding if that or that HTML-element should be visible.

Example:

if (type = 1)
{
    <img src="/Content/Images/gray.jpg" />
}
else
{
    <img src="/Content/Images/blue.jpg" />
}

I see several options here.
1. Use if-statements in my views (prefer not to)
2. Use a HTML-helper (will be a lot of helpers = Hard to work with the code)
3. Create HTML-snippets in my controller and pass into my view
4. Work with JavaScript to show/hide element on document load.

The example above if of course a simplification of my problem. Many thanks for the help!

/Mike

EDIT:
Took a bad example, here comes a new:

if (type = 1)
{
    <span class="class1">Something something</span>
}
else
{
    <div id="id1">
        text 
        <img src="/Content/Images/gray.jpg" />
    </div>
}
  • 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-20T07:54:24+00:00Added an answer on May 20, 2026 at 7:54 am

    I personally prefer to use HtmlHelper extensions or declarative HTML helpers for all but most trivial conditional expressions. However in this case I would probably let my laziness win, and express the conditional directly in the view like so:

    <img src="/Content/Images/@(i == 1 ? "gray" : "blue").jpg" />
    

    Edit

    For more complex scenarios, definitely HTML helpers. If you’re using the Razor syntax, you can create declarative helpers by placing them into your ~/Views/Helpers directory. They’re very clean to write and use.

    But the idea behind creating a good helper is that it encapsulates an intention. This is easy to test by trying to give your helper a name. If the name is IfOneThenShowSomethingElseGrayImage, you should try to break it into further pieces still.

    So let’s imagine the intention in your example is to show information about the current user based on the user type. You could create a helper like this:

    @helper UserType(int type) {
       @if (type == 1) {
        <span class="class1">Something something</span>
       } else {
        <div id="id1">
            text 
            <img src="/Content/Images/gray.jpg" />
        </div>
       }    
    }
    

    And then use it in your view like this:

    @UserType(Model.UserType)
    

    Even though it’s only used on one page, it enhances the readability in the view, and can be reused if necessary.

    Just my two cents.

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

Sidebar

Related Questions

Just started getting a bunch of errors on our C# .Net app that seemed
I just started thinking about creating/customizing a web crawler today, and know very little
I just started using the WPF WebBrowser that is included in Net 3.5 SP1.
I just started to learn Ruby and as a .Net developer, I'm wondering if
I just started using SVN, and I have a cache directory that I don't
I just started creating my data-access layer using LinqToSql. Everybody is talking about the
Just started learning NServiceBus and trying to understand the concept. When it talks about
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I just started using GNU Emacs as my text editor and I am concerned

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.