ASP.NET MVC: how to visualize another action (not view) using the model?

It is very easy to return another view from the controller:

return View("../Home/Info"); 

However, I need a model in the Information view. I have many things that happen in the result method of the Info () action. I can just copy it and do something like this:

 var infoModel = new InfoModel { // ... a lot of copied code here } return View("../Home/Info", infoModel); 

But this is not reasonable.

Of course, I can just redirect:

 return RedirecToAction("Info"); 

But that way the URL will change. I do not want to change the URL. It is very important.

+4
source share
3 answers

It looks like you want to trigger an action from another controller. I would suggest that you can simply visualize the view that displays this action with Html.Action () , instead of trying to link them together in a controller. If this is unreasonable, then you may need to create a base controller from which both controllers can get, and put the common code to generate the model in the base controller. Reuse the view as needed.

  public ActionResult Foo() { return View(); } 

Foo view

  @Html.Action( "info", "home" ) 
+8
source

You can call the right to another action from the action, for example:

 public ActionResult MyAction(){ if(somethingOrAnother){ return MyOtherAction(); } return View(); } //"WhichEverViewYouNeed" is required here since you are returning this view from another action //if you don't specify it, it would return the original action view public ActionResult MyOtherAction(){ return View("WhichEverViewYouNeed", new InfoModel{...}); } 
+9
source

Why not just call an action method?

+1
source

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


All Articles