c# – 如果没有,则避免太多,否则if语句为多个(9)条件

我的if else语句有9个条件,我在想是否还有其他替代解决方案来使代码干净简洁.所有9条件对asp.net mvc视图执行不同的计算.我已经包含了代码和一个显示某些条件视图的图像,希望它有意义,我正在为这个场景寻找更好更强大的解决方案..谢谢

//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;
}
最佳答案
首先,将表单值的提取移动到方法的开头,例如:

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

那么你可以很容易地设置属性而不需要任何ifs / else ifs,例如:

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

更新(关于您的评论/问题):

>语句x = condition中的?:运算符? value1:value2被称为conditional operator.如果condition为true,则返回value1,否则返回value2.
> int?是nullable integer,可以是整数值也可以是null.

转载注明原文:c# – 如果没有,则避免太多,否则if语句为多个(9)条件 - 代码日志