C# 面向对象的基本原理

C# 面向对象的基本原理,c#,model-view-controller,oop,C#,Model View Controller,Oop,我有以下代码: ViewData["someList"] = new[] { someOtherList }; foreach (var de in someOtherList ) { if (de.id == idMy) { ViewBag.Address = de.address; ViewBag.Name = de.name; ViewBag.Phone = de.phoneNumber;

我有以下代码:

ViewData["someList"] = new[] { someOtherList };
foreach (var de in someOtherList )
{
     if (de.id == idMy)
     {
          ViewBag.Address = de.address;
          ViewBag.Name = de.name;
          ViewBag.Phone = de.phoneNumber;
          ViewBag.Pin = de.pin;
          break;
     }
 }
下面是我如何定义someOtherList和idMy的方法:

public static List<MyClass> someOtherList;
private static int idEmployeeToDelete;

但一旦我输入该方法,idEmployeeToDelete为0,someOtherList为满,而其他时间idEmployeeToDelete为值,someOtherList为空。是否有任何方法,例如静态变量,某种容器,可以保存其中一个变量的值?

为什么要制作其他列表并使用idEmployeeToDelete static?@KMan我尝试将它们设置为非静态,结果是相同的。取出您的foreach语句并打印出您传递的变量/值。如果你能打印它们,那么它们就到达了你的目标。如果是,请再次尝试您的foreach代码片段。希望这有助于避免使用ViewBag,创建模型。你到底想做什么?您如何从静态方法中获得列表而不使用任何参数,可以显示代码吗?如果您想从列表中获取第一项,请使用.first而不是整个循环来获取第一项。您将在哪里分配给IdeEmployeeToDelete?在asp.net中使用静态变量对所有进入您站点的用户都是一样的,不要在其中添加ID,最后一个运行该方法的用户将覆盖所有其他用户的ID!!