4

It's really easy to return a different View from the Controller:

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

However, I need a model in the Info view. I have a lot of stuff going on in the Info() action result method. I can just copy it and have something like this:

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

But that is not reasonable.

Of course I can just redirect:

return RedirecToAction("Info");

But this way the URL will change. I don't want to change the URL. That's very important.

3 Answers 3

9

You can call right to another action from within an action, like this:

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's view
public ActionResult MyOtherAction(){
    return View("WhichEverViewYouNeed", new InfoModel{...});
}
Sign up to request clarification or add additional context in comments.

6 Comments

The view will correspond to the action that's invoked initially unless you specify which view you want explicitly.
@tvanfosson His question suggests he's already going to be specifying the view.
Yes, but in your example it will still look for the MyAction view even if MyOtherAction is called. You'd need to do a return View( "MyOtherAction", new InfoModel { ... } ); if you always wanted to render the view that goes along with it.
@tvanfosson Ahh, yes; I didn't realize I wasn't doing the same in my example. I'll fix that now.
No, you can't (shouldn't) just do that. See tvanfosson's answer for the easiest way to do that.
|
8

It looks like you want to invoke an action from a different controller. I'd suggest that you might want to simply render a view that renders that action using Html.Action() instead of trying to tie the two together in the controller. If that's unreasonable then you might want to create a base controller that both controllers can derive from and put the shared code to generate the model in base controller. Reuse the view as needed.

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

Foo View

  @Html.Action( "info", "home" ) 

2 Comments

Thanks! Just a small not: @{ Html.RenderAction(...); } is better performance wise.
does exactly what I wanted, hadn't thought of this.
1

Why not just invoke the method of the action?

2 Comments

He says he doesn't want the URL to change. (My biggest disappointment with MVC is that it's caused people to become obsessed with urls, often pointlessly.) Probably a partial view would work here.
@Ryan Invoking a different action method, as in my answer, would not change the URL. And I don't think it was MVC at all that got people obsessed with not changing URLs; do a search for Server.Transfer and see lots of WebForms obsession, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.