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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:10:06+00:00 2026-05-23T13:10:06+00:00

I have got 9 condition for my if else statement , I was thinking

  • 0

I have got 9 condition for my if else statement , I was thinking is there any other alternative solution to make the code clean and short. All the 9 condition perform different calculations for asp.net mvc view . I have included the code and an image which shows the view of some conditions, hope it make sense , I was looking for any better and robust solution for this scenario.. thanks

//Condition 1
//when no selection is made for the 2 dropdownlist (garages & helmets)
if (formvalues["helmets"] == "" && formvalues["garages"] == "")
{
    ViewBag.hideHelmet = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
    ViewBag.hideGarages = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
    ViewBag.SelectedHelmets = 1;//
    ViewBag.SelectedGarages = 1;//
    ViewBag.TotalHelmets = 1;
    ViewBag.TotalGarages = 1;
    ViewBag.TotalAmount = 1;
    ViewBag.TotalAmount =  trackEventCost.UnitCost;
}
//Condition 2
//When garages are selected from dropdown & helmets are not selected
else if ((formvalues["helmets"] == "") && (Convert.ToInt32(formvalues["garages"]) > 0))
{
    ViewBag.hideHelmet = 0;//for jquery, This value is passed to jquery script which then decides which field to hide/show
    ViewBag.hideGarages = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
    ViewBag.SelectedHelmets = 1;
    ViewBag.SelectedGarages = Convert.ToInt32(formvalues["garages"]);
    ViewBag.TotalHelmets = 1;
    ViewBag.TotalGarages = Convert.ToInt32(formvalues["garages"]) * getGarages.UnitCost;
    ViewBag.TotalAmount = ViewBag.TotalGarages + trackEventCost.UnitCost;
}
//Condition 3
//When helmets are selected from dropdown & garages are not selected
else if ((formvalues["garages"] == "") && (Convert.ToInt32(formvalues["helmets"]) > 0))
{
    ViewBag.hideHelmet = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
    ViewBag.hideGarages = 0;//for jquery, This value is passed to jquery script which then decides which field to hide/show
    ViewBag.SelectedGarages = 1;
    ViewBag.SelectedHelmets = Convert.ToInt32(formvalues["helmets"]);
    ViewBag.TotalGarages = 1;
    ViewBag.TotalHelmets = Convert.ToInt32(formvalues["helmets"]) * getGarages.UnitCost;
    ViewBag.TotalAmount = ViewBag.TotalHelmets + trackEventCost.UnitCost;
}
//Condition 4
//When garages are not selected from dropdown & helmets dropdownlist is hidden on the view due to unavailablity of helmets for that event
else if (formvalues["garages"] == "" && (formvalues["helmets"] == null))
{
    ViewBag.hideHelmet = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
    ViewBag.hideGarages = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
    ViewBag.TotalAmount = trackEventCost.UnitCost;
}
//Condition 5
//When helmets are not selected from dropdown & garages dropdownlist is hidden on the view due to unavailablity of garages for that event
else if ((formvalues["garages"] == null) && (formvalues["helmets"] == ""))
{
    ViewBag.hideHelmet = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
    ViewBag.hideGarages = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
    ViewBag.SelectedHelmets = 1;
    ViewBag.TotalAmount = trackEventCost.UnitCost;
}
//Condition 6
//When both helmets and garages dropdown list is not displayed on the view as they are not present in the database
else if (formvalues["helmets"] == null && formvalues["garages"] == null)
{
    ViewBag.SelectedHelmets = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
    ViewBag.SelectedGarages = 1;//for jquery, This value is passed to jquery script which then decides which field to hide/show
    ViewBag.TotalHelmets = 1;
    ViewBag.TotalGarages = 1;
    ViewBag.hideHelmet = 1;
    ViewBag.hideGarages = 1;
    ViewBag.TotalAmount = trackEventCost.UnitCost;
}
//Condition 7
//When garages are selected from dropdown & helmets dropdownlist is hidden on the view due to unavailablity of helmets for that event
else if (formvalues["helmets"] == null && Convert.ToInt32(formvalues["garages"]) > 0)
{
    ViewBag.hideHelmet = 0; //for jquery , This value is passed to jquery script which then decides which field to hide/show
    ViewBag.hideGarages = 1; //for jquery , This value is passed to jquery script which then decides which field to hide/show
    ViewBag.SelectedGarages = Convert.ToInt32(formvalues["garages"]);
    ViewBag.TotalGarages = Convert.ToInt32(formvalues["garages"]) * getGarages.UnitCost;
    ViewBag.TotalAmount = ViewBag.TotalGarages + trackEventCost.UnitCost;
}
//Condition 8
//When helmets are selected from dropdown & garages dropdownlist is hidden on the view due to unavailablity of garages for that event
else if (Convert.ToInt32(formvalues["helmets"]) > 0 && formvalues["garages"] == null)
{
    ViewBag.hideHelmet = 1; //for jquery , This value is passed to jquery script which then decides which field to hide/show
    ViewBag.hideGarages = 0; //for jquery , This value is passed to jquery script which then decides which field to hide/show
    ViewBag.TotalHelmets = Convert.ToInt32(formvalues["helmets"]) * getHelmets.UnitCost;
    ViewBag.TotalAmount = ViewBag.TotalHelmets + trackEventCost.UnitCost;
}
//Condition 9
//When garages and helmet both dropdown are selected
 else
{
    ViewBag.hideHelmet = 0;//for jquery , This value is passed to jquery script which then decides which field to hide/show
    ViewBag.hideGarages = 0;//for jquery , This value is passed to jquery script which then decides which field to hide/show
    ViewBag.SelectedHelmets = Convert.ToInt32(formvalues["helmets"]);
    ViewBag.SelectedGarages = Convert.ToInt32(formvalues["garages"]);
    ViewBag.TotalHelmets = Convert.ToInt32(formvalues["helmets"]) * getHelmets.UnitCost;
    ViewBag.TotalGarages = Convert.ToInt32(formvalues["garages"]) * getGarages.UnitCost;
    ViewBag.TotalAmount = ViewBag.TotalHelmets + ViewBag.TotalGarages + trackEventCost.UnitCost;
}

Some Conditions

  • 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-23T13:10:06+00:00Added an answer on May 23, 2026 at 1:10 pm

    First, move the extraction of the form value to the beginning of the method, e.g:

    int? helmets = formvalues["helmets"] == null ?
      null : Convert.ToInt32(formvalues["helmets"];
    int? garages = formvalues["garages"] == null ?
      null : Convert.ToInt32(formvalues["garages"];
    

    Then you can probably very easily set the properties without any ifs/else ifs, e.g:

    ViewBag.hideHelmet = helmets;
    // or
    someOtherProperty = helmets == null ? ... : ...
    

    Update (regarding your comment/question):

    • The ?: operator in the statement x = condition ? value1 : value2 is called conditional operator. It returns value1 if condition is true, and value2 otherwise.
    • int? is a nullable integer, which can either have an integer value or be null.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got the following IF condition code: if ((depth <= min_depth ) &&
Have got an NSString *str = @12345.6789 and want to find out if there
We have got a custom MembershipProvider in ASP.NET . Now there are 2 possible
I have got some code to load an assembly and get all types, which
I have got the following code from here to read an Excel file using
i have been trying to make a bouncing ball animation.I have got everything right
I have got a strange compile error while using condition operator. a,b are int
I have this in my tableView:cellForRowAtIndexPath: if (condition) { // make a custom cell
I have this bit of code thats got me crazy. Its a cellForRowAtIndexPath displaying
We have got loads of options for php + MySQL + Apache combo... Which

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.