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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:40:29+00:00 2026-05-26T03:40:29+00:00

If I wanted multiple if statements in an html attribute I might do something

  • 0

If I wanted multiple if statements in an html attribute I might do something like this:

  <input type="button" value="Bad, the title has a lot of excess spacing" title="@if(SomeModel.condOne) {
                              <text>this</text>
                              }
                              @if (SomeModel.CondTwo)
                              {
                              <text> is</text>
                              }
                              @if (SomeModel.CondThree)
                              {
                              <text> a title</text>
                              }      
                             " />

But that creates a lot of empty spaces that need truncating. So this works:

  <input type="button" value="Good, the title is condenced" title="@if(SomeModel.condOne) {<text>this</text>}@if (SomeModel.CondTwo){<text> is</text>}@if (SomeModel.CondThree){<text> a title</text>}" />

The same principle can be applied to an element with multiple classes (e.g. class=”oddrow class1″ -> class=”evenrow class2″)

But that might be hard to read if it’s a long line. And visual studio has a habit of breaking that statement into multiple lines if you touch the bracket or Ctrl-K,Ctrl-D (which any next developer is likely to do).

Is there a better or more fullproof way to implement multiple attribute conditions in a line for MVC razor?

  • 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-26T03:40:30+00:00Added an answer on May 26, 2026 at 3:40 am

    I suggest creating a small helpers method that returns the text you need.

    You’d have to pass it SomeModel and inside that method check for your condition that way you’d have something nicer to look at and easier to maintain.

    For example:

    public static class HtmlHelpers
    {
        public static string FetchTitle(this HtmlHelper helper, SomeModel model)
        {
            //Your logic here.
        }
    }
    

    You can read all about Html Helper methods here on Jon Galloway’s blog.

    That’s where I learned how to use them.

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

Sidebar

Related Questions

If I wanted to fetch multiple ROWS without doing the while loop like this:
I wanted to know, how to evaluate multiple statements in a function in Mathematica.
This is a follow-up of this question . I wanted to plot multiple curves
I have a SQL table containing train schedules. The table looks something like this:
I wanted to run a query-replace-regexp across multiple files in emacs, and found this
I set up a Zend Framework application using Zend_Tool, but I wanted multiple modules
If I wanted to have Python distributed across multiple processors on multiple computers, what
If you wanted to start a 'website' that could be accessible from multiple 'endpoints'.
I wanted to know if there was a way to shorten this if statement
I'm trying to implement a facebook like like relationship. I wanted to use inheritance

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.