简体   繁体   中英

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. I want to use this value in another class through the instance of this model class.

At run time of the method, this instance wasnt null, also in other classes. But after the method runned through, i cannot use the added value in the other class because the instance is null. How can i use this instance with the added value?

Add value method in 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:

public Events _events;

This is because the field _events only last the duration of the instance of your class. Each new web request constructs a new controller to perform the task and then the controller is destructed upon the request completion.

Because property with the same declaration but in a different class is different from each other.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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