how to make foreach loop in MVC to set new value to items.

前端 未结 1 536
太阳男子
太阳男子 2021-01-29 11:32

im trying to do loop throw objects in clas and check its data tybe and make control, if the data type is string so i want this item value to be empty. and if the data type is i

1条回答
  •  一整个雨季
    2021-01-29 12:26

    The foreach can iterate only on collections! The i variable in this case may be a list like this:

     List i = new List();
    

    Now you can iterate on it. The foreach iterate through the list:

     foreach( ProductionOrderItem item in i)
            {
                  if(item.data type is string)
                  {
                      item.value = "" ;
                  }
                  if ((item.data type is int)
                  {
                    item.value = 0 ;
                  }
    
                  if (item.data type is stringDateTime))
                  {
                      item.value = 2011-01-01 00:00:00;
                  }
               else
                   // do any thing. 
    
            }
    

    But remember: don't change the variable i within the loop.

    0 讨论(0)
提交回复
热议问题