Is there a way to access the user assistant from the controller?

I am creating this MVC3 Razor ASP.NET application in which I use a custom WebViewPage that inherits from this. Thus, all my views use my custom WebViewPage:

public class MyCustomWebViewPage : System.Web.Mvc.WebViewPage { public CustomHelper MyHelper { get; private set; } : } 

And then in the web.config located in the Views folder, I pointed out that MyCustomViewPage is my default base page.

 <system.web.webPages.razor> <pages pageBaseType="Namespace.MyCustomWebViewPage"> <namespaces> : </namespaces> </pages> </system.web.webPages.razor> 

So far so good, on the View.cshtml pages I can access my custom helper from razor markup:

 @MyHelper.SomeMethod() 

Now there is something that I need to do conditionally on the assistant. I tried to set the helper property in the @ {} section of CSHTML startup, but it's too late to account for it.

So, I'm going to do this either on the called Action method, or override the OnActionExecuting () method.

The problem is that in the controller I did not find a way to access my user assistant, for example:

 public class AnyController : Controller { public ActionResult Index() { MyHelper.SomeProperty = true; } } 

To explain this a bit more, the concept is that in the action (action code in the controller) I want to set a property in the user assistant that will allow the use of certain functions. Thus, it does not appear on representations that it does not need. It differs from the @section concept, in which the content is defined in the view, but rather says: “I want this function to be added to the layout”, and the layout produced some predefined markup that would allow this function AND, which in several places in the view can be “nuggets”, which require “function support markup” or a script. If I use a section, this “general” script will be displayed several times, and this is not what I need.

Having said that, how can I access the (custom) helper defined on the base view page inside the controller?

+4
source share
1 answer

Consider transferring information through the model, as it is more consistent with MVC. You can use a strongly typed model or a weakly typed ViewBag (sample - Transfer data from the controller for viewing ).

The controller does not need to know which page will be used to render the presentation. He just says something, "find a view with that name and display that data (model)." Depending on different conditions, there may be different views corresponding to the same name, so usually you have no way to set the properties of a view object from the controller.

Edit: why it is technically very difficult:

The lifetime of the action method call ( ActionResult Index(){...} ) does not overlap with the page lifetime (WebViewPage object). WebViewPage created after the action completed. Therefore, if you are really interested in transferring data from an action directly to a view object (and not through built-in mechanisms), you will need to figure out how to pass a part of the code (probably in the form of a delegate) to view the creation code.

+2
source

Source: https://habr.com/ru/post/1415485/


All Articles