K
K
kostyabkb2019-02-12 23:58:35
ASP.NET
kostyabkb, 2019-02-12 23:58:35

How to update the data returned by the view to the controller?

there is a construct like
Options _options;
HomeController(IOptions options)
{_options = options;}
void ActionResult Index(Options options)
{_options = options
Redirect...}
... Each time the homepage is navigated, the controller's constructor restarts and overwrites _options, preventing them from most make the changes created in the view and controller. How can this be bypassed?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
eRKa, 2019-02-13
@kttotto

You need to study the ASP MVC lifecycle to understand that a controller instance lives exactly from request to response. With each new request, a new instance of the controller is created, which means that

void ActionResult Index(Options options)
{_options = options }

does not make sense, with a new request, _options will be empty, even if the constructor does not have a parameter.
If you want to save some settings via ActionResult Index(Options options), then create a global static class to store some parameters for all requests. If these settings are individual for each user, then you can store them in sessions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question