简体   繁体   English

class 的实例是 null 在其他 class

[英]Instance of class is null in other class

I added a value to a property of my Events.cs model class in a method of my controller class.我在我的Events.cs ZA2F2ED4FDC40AB61DZC2 的方法中为我的 Events.cs model class 的属性添加了一个值。 I want to use this value in another class through the instance of this model class.我想通过这个 model class 的实例在另一个 class 中使用这个值。

At run time of the method, this instance wasnt null, also in other classes.在该方法的运行时,此实例不是 null,也在其他类中。 But after the method runned through, i cannot use the added value in the other class because the instance is null.但是在方法运行之后,我不能使用其他 class 中的附加值,因为实例是 null。 How can i use this instance with the added value?我如何使用这个具有附加值的实例?

Add value method in Controller class: Controller class中的加值方法:

public Events _events;

[HttpPost]
public ActionResult Search(Events events)
{
    _events = events;
    return Json("true", JsonRequestBehavior.AllowGet);
}

Instance of Events.cs in other class:其他Events.cs中的 Events.cs 实例:

public Events _events;

This is because the field _events only last the duration of the instance of your class.这是因为字段_events仅持续您的 class 实例的持续时间。 Each new web request constructs a new controller to perform the task and then the controller is destructed upon the request completion.每个新的 web 请求都会构造一个新的 controller 来执行任务,然后在请求完成时销毁 controller。

Because property with the same declaration but in a different class is different from each other.因为具有相同声明但在不同 class 中的属性彼此不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM