如何使foreach循环在MVC新的值设置为项目。 [英] how to make foreach loop in MVC to set new value to items.

查看:98
本文介绍了如何使foreach循环在MVC新的值设置为项目。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试做环抛物体CLAS,并检查其数据tybe
并进行控制,如果数据类型为字符串,所以我想这个项目值是空的。
如果数据类型是int所以这个项目的值是0,依此类推。

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 int so the value of this item is 0 and so on.

我在这里的伪code

here my pseudo code

 ProductionOrderItem i = new ProductionOrderItem();

 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. 

        }

或是否有可能把项目插入阵列中,并使LOPP虽然个数组?

or is it possible to put the item into array and make lopp though th array ?

推荐答案

在foreach只能在集合迭代!在这种情况下,变量i可以是这样的列表:

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

 List<ProductionOrderItem> i = new List<ProductionOrderItem>();

现在可以在其上进行迭代。在列表中的foreach迭代:

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. 

        }

但要记住:不改变循环中的变量i

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

这篇关于如何使foreach循环在MVC新的值设置为项目。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆