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 3636288
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:00:44+00:00 2026-05-19T01:00:44+00:00

Hopefully this question is quick and painless I have a mvc view where i

  • 0

Hopefully this question is quick and painless

I have a mvc view where i want to display either one of two values depending on an if statement. This is what I have in the view itself:

 <%if (model.CountryId == model.CountryId) %>
        <%= Html.Encode(model.LocalComment)%> 
        <%= Html.Encode(model.IntComment)%>

If true display model.LocalComment, if false display model.IntComment.

This doesn’t work as I get both values showing. What am I doing wrong?

  • 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-19T01:00:45+00:00Added an answer on May 19, 2026 at 1:00 am

    Your if statement always evaluates to true. You are testing whether model.CountryId equals model.CountryId which is always true: if (model.CountryId == model.CountryId). Also you are missing an else statement. It should be like this:

    <%if (model.CountryId == 1) { %>
        <%= Html.Encode(model.LocalComment) %> 
    <% } else if (model.CountryId == 2) { %>
        <%= Html.Encode(model.IntComment) %>
    <% } %>
    

    Obviously you need to replace 1 and 2 with the proper values.

    Personally I would write an HTML helper for this task to avoid the tag soup in the views:

    public static MvcHtmlString Comment(this HtmlHelper<YourModelType> htmlHelper)
    {
        var model = htmlHelper.ViewData.Model;
        if (model.CountryId == 1)
        {
            return MvcHtmlString.Create(model.LocalComment);
        } 
        else if (model.CountryId == 2)
        {
            return MvcHtmlString.Create(model.IntComment);
        }
        return MvcHtmlString.Empty;
    }
    

    And then in your view simply:

    <%= Html.Comment() %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just a quick question (hopefully). I want to slide one div in front of
Hopefully a very quick question. In one of my functions I want to generate
I have a (hopefully) quick question. I have two dates stored in a database,
I have a (hopefully) quick question. I've got some stepper boxes. Though really this
Just a quick regex question...hopefully I have a string that looks something like this:
I can't see this question anywhere else, it's hopefully a quick and easy one.
Hopefully this is a quick and easy question to answer. I have a MySQL
Quick question, hopefully I am just missing something simple. Ok I have one class
Hopefully this is a quick question. I'm making a simple rock paper scissors game.
Hopefully this question isn't as subjective as I think it may be. I have

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.