Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc Can i have one view for multiple action methods to target? or do i need a separate view for each?

Tags:

asp.net-mvc

I have set up a menu-controller to drive the top menu links based on which other controller is being used. Each other controller has a separate nested master page for each of its views.

so, i have a menu-controller with several methods that return viewresults, one per each controller, or "section" of the site. So currently each of these methods has its own view to render the menu. but each view to render the menu is the same code, the only thing that changes is the logic in the controller methods based on which links to render.

is there any way to have all of these controller actions target the same view? since the view is the same for all?

thanks

like image 365
robert j Avatar asked Feb 18 '10 17:02

robert j


People also ask

Can one action method have multiple views?

Yes, completely possible. And, it can be multiple views, or even a FileResult or other result type.

Can we use two multiple models with a single view?

Introduction. In MVC we cannot pass multiple models from a controller to the single view.

Can we have two action methods with same name in MVC?

While ASP.NET MVC will allow you to have two actions with the same name, . NET won't allow you to have two methods with the same signature - i.e. the same name and parameters. You will need to name the methods differently use the ActionName attribute to tell ASP.NET MVC that they're actually the same action.

How is action method linked to the view?

The following illustrates the Index() action method in the StudentController class. As you can see in the above figure, the Index() method is public, and it returns the ActionResult using the View() method. The View() method is defined in the Controller base class, which returns the appropriate ActionResult .


2 Answers

Yes, that is a common practice.

return View("Menu");
like image 136
Francisco Aquino Avatar answered Sep 24 '22 07:09

Francisco Aquino


Create a strongly typed view that takes a container specifying your menu content. Pass this as a parameter on your return statement.

 var thisMenu = CreateMenuForThisRequest();
 return View ("Menu", thisMenu);
like image 36
No Refunds No Returns Avatar answered Sep 25 '22 07:09

No Refunds No Returns