我的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;
}

发布于 2011-07-01 21:05:01
首先,将表单值的提取移到方法的开头,例如:
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,并且value2 value1为nullable integer,则返回null,该值可以是整数值,也可以是null。发布于 2011-07-01 20:57:10
当然,你可以做的一件事就是改进你写条件句的方式。我认为将条件提取到方法中来描述检查的实际操作可能会更好,而不是到处添加注释。
发布于 2011-07-01 21:00:04
为什么你不关注分配目标,而不是条件呢?
条件看起来很简单,而且是重复的。一个例子:
ViewBag.hideHelmet = formvalues["helmets"] == ""? 1 : 0;这不需要任何分支逻辑。
https://stackoverflow.com/questions/6548207
复制相似问题