私有成员在控制器返回空行动结果被设置后, [英] private member in controller returns null after being set in action result

查看:112
本文介绍了私有成员在控制器返回空行动结果被设置后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET MVC应用程序控制器。

I have a controller in ASP.NET MVC application.

private GipDbContext db = new GipDbContext();
private Employee employeeObj;

public ActionResult Edit(int? id)
        {
            Employee employee = db.Employees.Find(id);

            //employeeObj SET TO ANOTHER OBJECT
            employeeObj = employee;

            return View(employee);
        }

public PartialViewResult TimeSeriesData(int? tsdataid)
        {
            TimeSeriesData tsobject = new TimeSeriesData();

            // employeeObj RETURNING NULL
            foreach (var item in employeeObj.TimeSeriesData){
                if (item.TimeSeriesDataID == tsdataid)
                {
                    tsobject = item;
                    break;
                }
            }

被称为第一种方法是编辑,然后当TimeSeriesData被称为employeeObj被返回null,即使是在编辑方法设置..任何想法,为什么?

The first method being called is Edit, then when TimeSeriesData is called employeeObj is returning null, even though it was set in the Edit method.. any ideas why?

推荐答案

控制器被实例为每个请求。一旦你要求 TimeSeriesData ,这是为请求创建编辑器已被释放。如果您需要扶住了一块请求之间的数据,您必须把它放在会话的TempData (两者的其中使用会话)。

Controllers are instantiated for each request. Once you request TimeSeriesData, the controller that was created for the request to Edit has already been disposed. If you need to hold on to a piece of data between requests, you must either put it in Session or TempData (both of which use sessions).

这篇关于私有成员在控制器返回空行动结果被设置后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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