2011-07-01 44 views
3

我有我的if else語句的條件,我想有沒有其他的替代解決方案,使代碼乾淨和短。所有9條件對asp.net mvc視圖執行不同的計算。我已經包括了代碼和圖像,顯示的一些情況來看,希望這是有意義的,我一直在尋找什麼更好的和可靠的解決方案對於這種情況..謝謝避免太多,否則,否則,如果聲明多個(9)條件

//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

+0

它看起來像頭盔發生的事情不會改變任何你做的車庫(獨立操作=>使用2個案例陳述)。所以你只需要做3 + 3 = 6支票而不是3 * 3 = 9支票。也困惑......你有兩個不同的變量具有相同的註釋,它是唯一看起來不同的空字符串和空情況。 –

+0

我建議你研究一下如何避免太多ifs,因爲這是你的第二個問題。 – Reniuz

+0

@yeh Renius,但我沒有得到更好的答案,所以我簡單解釋了這一個與圖像,nd得到太多的答案,將研究所有的解決方案thnx每個人。 –

回答

3

首先,移動窗體值的提取到方法的開頭,如:

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

然後你可以或許很容易設置的屬性沒有任何IFS /其他IFS,例如:

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

更新(關於你的評論/問題):

  • 在聲明x = condition ? value1 : value2中的?:運營商稱爲conditional operator。如果condition爲真,則返回value1,否則返回value2
  • int?是一個nullable integer,它可以有一個整數值或爲空。
+0

你能解釋一下我的聲明:int?頭盔= formvalues [「頭盔」] == null? null:Convert.ToInt32(formvalues [「helmets」]; –

+1

'int?'是一個可爲空的整數:如果沒有找到帶有「helmets」鍵的formvalue,則將'helmets'設置爲null,否則設置爲formvalues中的值 – M4N

+1

然後你可以檢查頭盔是否有值:'如果頭盔有.HasValue ...' – M4N

5

當然1你可以做的事情是改善你寫條件的方式。我認爲你可以通過將條件提取到方法來描述支票的實際操作,而不是在任何地方添加評論,這樣可以更好地服務。

+1

OP可以將if塊中的語句提取到自己的方法中。這將顯着清理代碼。 – FishBasketGordo

+0

所有條件都做了一些計算,例如在條件1中,沒有選擇任何內容,因此不進行計算,並且在發佈頁面時都會隱藏下拉列表。也是唯一的總價格數量沒有任何額外的價格的頭盔或車庫,因爲他們沒有選擇。 –

2

爲什麼不把注意力集中在分配目標而不是條件上?

該condidition似乎很簡單,重複。一個樣本:

ViewBag.hideHelmet = formvalues["helmets"] == ""? 1 : 0; 

這不需要任何分支邏輯。

+0

你能解釋一下你使用的運營商嗎? –

+0

@Mr A:'條件? first_expression:second_expression;'是[條件運算符](http://msdn.microsoft.com/en-us/library/ty67wk28(v = vs.80).aspx) – sehe

1

,因爲你沒有在所有情況下都設置的值你可以與某些參數移動

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; 

要在方法optional。其次,你可以使用ternary operator。你可以使用像三元運算符。

int r = 1; 
    long l = r == 1 ? 1 : (r==2 ? 2 : (r==3?3:(r==4 ? 4 : 5))); 
+0

你有三元運算符的任何gud教程 –

+0

請看我答案中的鏈接。 –

+0

您的鏈接正在重定向到您的個人資料 –

1

您可以將您的ViewBag抽象爲設置所有數據的函數。這會稍微清理一些東西。

if(...) { 
    SetUpViewBag(...); } 
else { 
    SetUpViewBag(...); } 

... 

private SetUpViewBag(...) 
{ 
    ViewBag.SelectedHelmets = prop1; 
    ViewBag.SelectedGarages = prop2; 
    ViewBag.TotalHelmets = prop3; 
    ViewBag.TotalGarages = prop4; 
    ViewBag.hideHelmet = prop5; 
    ViewBag.hideGarages = prop6; 
    ViewBag.TotalAmount = prop7; 
} 
1

我會用Factory Pattern根據條件來生成ViewBag對象。讓工廠生成所需的ViewBag並將您的對象分配給該對象。

這裏是在一個控制檯的例子(只是爲了快速演示)的例子:

class MainApp 
    { 
    /// <summary> 
    /// Entry point into console application. 
    /// </summary> 
    static void Main() 
    { 
     ViewBagCreator creator = new ConcreteCreatorForViewBagObject(); 

     string garages_form_value = ""; // formvalues["garages"]; 
     string helmets_form_value = ""; // formvalues["helments"]; 

     // create viewpage 
     ViewBagObject view_bag = creator.FactoryMethod(helmets_form_value, 
      garages_form_value, 10, 100, 23); 
     Console.WriteLine("Created {0}", 
      view_bag.GetType().Name); 

     // Assign your viewbag object here 
     // ViewBag = view_bag; 

     // Wait for user 
     Console.ReadKey(); 
    } 
    } 

    /// <summary> 
    /// The 'ViewBagObject' abstract class 
    /// </summary> 
    abstract class ViewBagObject 
    { 
    public int hideHelmet; 
    public int hideGarages; 
    public int SelectedHelmets; 
    public int SelectedGarages; 
    public int TotalHelmets; 
    public int TotalGarages; 
    public int TotalAmount; 
    } 

    /// <summary> 
    /// A 'ViewBagNoSelection' class 
    /// </summary> 
    class ViewBagNoSelection : ViewBagObject 
    { 
    public ViewBagNoSelection(int trackEventUnitCost) 
    { 
     hideHelmet = 1; 
     hideGarages = 1; 
     SelectedHelmets = 1; 
     SelectedGarages = 1; 
     TotalHelmets = 1; 
     TotalGarages = 1; 
     TotalAmount = trackEventUnitCost; 
    } 
    } 

    /// <summary> 
    /// A 'ViewBagGaragesNoHelments' class 
    /// </summary> 
    class ViewBagGaragesNoHelments : ViewBagObject 
    { 
    public ViewBagGaragesNoHelments(int garagesValue, int garagesUnitCost, int trackEventUnitCost) 
    { 
     hideHelmet = 0; 
     hideGarages = 1; 
     SelectedHelmets = 1; 
     SelectedGarages = garagesValue; 
     TotalHelmets = 1; 
     TotalGarages = garagesValue * garagesUnitCost; 
     TotalAmount = TotalGarages * trackEventUnitCost; 
    } 
    } 

    /// <summary> 
    /// A 'ViewBagHelmentsNoGarages' class 
    /// </summary> 
    class ViewBagHelmentsNoGarages : ViewBagObject 
    { 
    public ViewBagHelmentsNoGarages(int helmetsValue, int helmentsUnitCost, int trackEventUnitCost) 
    { 
     hideHelmet = 0; 
     hideGarages = 1; 
     SelectedHelmets = helmetsValue; 
     SelectedGarages = 1; 
     TotalHelmets = helmetsValue * helmentsUnitCost; 
     TotalGarages = 1; 
     TotalAmount = TotalHelmets * trackEventUnitCost; 
    } 
    } 

    /// <summary> 
    /// The 'ViewBagCreator' abstract class 
    /// </summary> 
    abstract class ViewBagCreator 
    { 
    public abstract ViewBagObject FactoryMethod(
     string helmetsFormValue, string garagesFormValue, 
     int helmetsUnitCost, int garagesUnitCost, int trackEventUnitCost); 
    } 

    /// <summary> 
    /// A 'ConcreteCreator' class 
    /// </summary> 
    class ConcreteCreatorForViewBagObject : ViewBagCreator 
    { 
    public override ViewBagObject FactoryMethod(
     string helmetsFormValue, string garagesFormValue, 
     int helmetsUnitCost, int garagesUnitCost, int trackEventUnitCost) 
    { 
     bool helmets_value_is_null = (helmetsFormValue == null); 
     bool helmets_value_is_empty = (helmetsFormValue == ""); 
     bool garages_value_is_null = (garagesFormValue == null); 
     bool garages_value_is_empty = (garagesFormValue == ""); 

     int helmets = 0; 
     int garages = 0; 
     int.TryParse(garagesFormValue, out garages); 
     int.TryParse(helmetsFormValue, out helmets); 

     bool garages_greater_than_zero = garages > 0; 
     bool helmets_greater_than_zero = helmets > 0; 

     if (helmets_value_is_empty && garages_value_is_empty) 
     { 
      return new ViewBagNoSelection(trackEventUnitCost); 
     } 
     else if (helmets_value_is_empty && garages_greater_than_zero) 
     { 
      return new ViewBagGaragesNoHelments(garages, garagesUnitCost, trackEventUnitCost); 
     } 
     else if (garages_value_is_empty && helmets_greater_than_zero) 
     { 
      return new ViewBagHelmentsNoGarages(helmets, helmetsUnitCost, trackEventUnitCost); 
     } 
     //... 
     return null; 
    } 
    } 
1

看看在Strategy模式,對於你的情況,你必須使用每個戰略每個策略的方法,而不是類。

void ApplyStrategy(int? helmets, int? garages) 
{ 
// depends on values of helmets and garages execute appropriate strategy 
} 

void ApplyNoHelmetsNoGaragesStrategy() 
void ApplyNoGaragesStrategy() 
void ApplyNoHelmetsStrategy() 
// ... 
+0

雖然您對策略模式是正確的,但我認爲這不會對OP有所幫助。嘗試給出OP如何用策略模式重構他的代碼的例子。 – Steven

+0

太多的答案,我必須通過所有的答案,看看哪一個更好,我希望我是在csharp gud .. –

1

怎麼樣構建一個條件類

public class Condition 
{ 
    public Func<bool> Match { get; set; } 
    public Action Execute { get; set; } 
} 

和bulid列表

 var conditions = new List<Condition> 
     { 
      new Condition 
       { 
        Match =() => formvalues["helmets"] == "" && formvalues["garages"] == "", 
        Action =() => 
           { 
             ViewBag.hideHelmet = 1;  
             ViewBag.hideGarages = 1; 
             ViewBag.SelectedHelmets = 1; 
             ... 
           } 
       }, 
      new Condition 
       { 
        ... 
       } 
     }; 

然後LINQ:

conditions.Where(c => c.Match()).ToList().ForEach(c => c.Execute()); 
1

一些GRE在建議。我喜歡處理這些情況如下。我重構了您的代碼以完全刪除條件。相反,我已經將每個條件的代碼封裝到一個方法中,並使用int類型的鍵和Action類型的值創建了一個散列表。我已經爲每個頭盔/車庫組合派生出一個獨特的價值,然後將其用作相應方法的關鍵。

我已經包含了不在你的文章中的代碼位的虛擬實現,以便我可以編譯和運行我的重構示例。希望有所幫助。

using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.IO; 

namespace StackOverflow 
{ 
    public enum HelmetState { Null = 1, Empty = 2, HasValue = 3 } 
    public enum GarageState { Null = 4, Empty = 5, HasValue = 6 } 

    public class ReducingConditionals 
    { 
     private TrackEventCost trackEventCost = new TrackEventCost(); 
     private GetHelmets getHelmets = new GetHelmets(); 
     private GetGarages getGarages = new GetGarages(); 
     private IDictionary<int, Action> ExecuteCondition; 

     //the formvalues collection 
     private Hashtable _formvalues; 
     public Hashtable formvalues{ get { return _formvalues ?? (_formvalues = new Hashtable()); } } 

     //set up dictionary of Actions 
     public ReducingConditionals() 
     { 
      ExecuteCondition = new Dictionary<int, Action>(); 
      ExecuteCondition[Key(HelmetState.Null, GarageState.Null)] = HelmetsNullGaragesNull; 
      ExecuteCondition[Key(HelmetState.Null, GarageState.Empty)] = HelmetsNullGaragesEmpty; 
      ExecuteCondition[Key(HelmetState.Null, GarageState.HasValue)] = HelmetsNullGaragesHasValue; 
      ExecuteCondition[Key(HelmetState.Empty, GarageState.Null)] = HelmetsEmptyGaragesNull; 
      ExecuteCondition[Key(HelmetState.Empty, GarageState.Empty)] = HelmetsEmptyGaragesEmpty; 
      ExecuteCondition[Key(HelmetState.Empty, GarageState.Empty)] = HelmetsEmptyGaragesHasValue; 
      ExecuteCondition[Key(HelmetState.HasValue, GarageState.Empty)] = HelmetsHasValueGaragesNull; 
      ExecuteCondition[Key(HelmetState.HasValue, GarageState.Null)] = HelmetsHasValueGaragesEmpty; 
      ExecuteCondition[Key(HelmetState.HasValue, GarageState.HasValue)] = AnyOtherCondition; 
     } 

     //gets a unique value for each HelmetState/GarageState combination to be used as a key to the dictionary 
     private int Key(HelmetState helmetState, GarageState garageState) 
     { 
      return (int)helmetState + (int)garageState; 
     } 

     //Execute the appropriate method - n.b. no if statements in sight! 
     public void DealWithConditions() 
     { 
      HelmetState helmetState = GetHelmetState(formvalues["helmets"]); 
      GarageState garageState = GetGarageState(formvalues["garages"]); 
      ExecuteCondition[Key(helmetState, garageState)](); 
     } 

     //assign helmet state enum 
     private HelmetState GetHelmetState(object helmetValue) 
     { 
      if (helmetValue == null) return HelmetState.Null; 
      if (helmetValue.ToString() == "") return HelmetState.Empty; 
      if (Convert.ToInt32(helmetValue) > 0) return HelmetState.HasValue; 
      throw new InvalidDataException("Unexpected parameter value"); 
     } 

     //assign garage state enum 
     private GarageState GetGarageState(object garageValue) 
     { 
      if (garageValue == null) return GarageState.Null; 
      if (garageValue.ToString() == "") return GarageState.Empty; 
      if (Convert.ToInt32(garageValue) > 0) return GarageState.HasValue; 
      throw new InvalidDataException("Unexpected parameter value"); 
     } 

     #region encapsulate conditions in methods 
     private void AnyOtherCondition() 
     { 
      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; 
     } 

     private void HelmetsHasValueGaragesNull() 
     { 
      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; 
     } 

     private void HelmetsNullGaragesHasValue() 
     { 
      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; 
     } 

     private void HelmetsNullGaragesNull() 
     { 
      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; 
     } 

     private void HelmetsEmptyGaragesNull() 
     { 
      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; 
     } 

     private void HelmetsNullGaragesEmpty() 
     { 
      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; 
     } 

     private void HelmetsHasValueGaragesEmpty() 
     { 
      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; 
     } 

     private void HelmetsEmptyGaragesHasValue() 
     { 
      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; 
     } 

     private void HelmetsEmptyGaragesEmpty() 
     { 
      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; 
     } 
     #endregion 
    } 

    #region dummy class class implementations 
    public class ViewBag 
    { 
     public static int TotalAmount; 
     public static int hideHelmet { get; set; } 
     public static int hideGarages { get; set; } 
     public static int SelectedHelmets { get; set; } 
     public static int SelectedGarages { get; set; } 
     public static int TotalGarages { get; set; } 
     public static int TotalHelmets { get; set; } 
    } 
    internal class GetGarages { public int UnitCost; } 
    internal class GetHelmets { public int UnitCost; } 
    internal class TrackEventCost{ public int UnitCost;} 
    #endregion 
}