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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:09:37+00:00 2026-05-26T17:09:37+00:00

The variable strCSSClass often has a value but sometimes is empty. I do not

  • 0

The variable strCSSClass often has a value but sometimes is empty.

I do not want to include an empty class=”” in this input element’s HTML, which means if strCSSClass is empty, I don’t want the class= attribute at all.

The following is one way to do a conditional HTML attribute:

<input type="text" id="@strElementID" @(CSSClass.IsEmpty() ? "" : "class=" + strCSSClass) />

Is there a more elegant way of doing this? Specifically one where I could follow the same syntax as is used in the other parts of the element: class=”@strCSSClass” ?

  • 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-26T17:09:37+00:00Added an answer on May 26, 2026 at 5:09 pm

    You didn’t hear it from me, the PM for Razor, but in Razor 2 (Web Pages 2 and MVC 4) we’ll have conditional attributes built into Razor (as of MVC 4 RC tested successfully), so you can write things like this:

    <input type="text" id="@strElementID" class="@strCSSClass" />
    

    If strCSSClass is null then the class attribute won’t render at all.

    Further Reading

    • Jon Galloway – ASP.NET MVC 4 Beta Released!
    • Conditional Attributes in Razor View Engine and ASP.NET MVC 4
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting 'variable is undefined', I'm guessing this has something to do with the
My variable $var has the form 'abc.de'. What does this substr exactly do in
Variable resp has following value. System.Xml.Linq.XElement resp - How do I get value of
The variable checkedones is not passing to the AJAX.Updater. The item.value is reporting a
The variable filepath which is a string contains the value Música . I have
I have a perl variable $results that gets returned from a service. The value
How can I unset variable? For example, PHP has an unset($var) function.
$image variable gives this code (when echo is used): <img src=/image.png width=100 height=147 alt=
double variable = Convert.ToDouble(5/100); Will return 0.0 but i expected 0.05 What can /
Variable variables seem pretty cool, but I can't think of a scenario where one

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.