MVC Action Method - Multiple Views - asp.net-mvc

Is it possible for a single action method to render two different views subsequently. And if possible pick the user input from the first view and use it on the second view?

Yes, completely possible. And, it can be multiple views, or even a FileResult or other result type.
public ActionResult MyAction() {
if(/*logic here*/)
return View("ViewOne");
else
return View("ViewTwo");
}

Yes, it's possible. Just make sure your views have same view model.
To switch between views you can specify parameter:
return View("MyFirstView", viewModel);
or
return View("MySecondView", viewModel);

Related

MVC define #section for all views in area

I have a container in my layout where I'd like to display a list of menu items, and I'd like for additional menu items to be displayed based on which area of the site we are viewing.
I know that I could do this with a #section, but that means I would have to copypaste the section contents in every single view within the area, which would be a maintenance mess and a nasty violation of DRY. Multiple per-area layouts would also be undesirable code duplication.
It would be good to do it in the _ViewStart partial, but apparently MVC doesn't allow #sections to be defined in partials. What else can I do?
I would consider making an action method in some generic controller that returns the correct partial view with the proper menu items.
[ChildActionOnly]
public PartialViewResult GetSubMenu(){
var areaName = ViewContext.RouteData.DataTokens["area"];
switch(areaName){
case "Admin":
return PartialView("_adminSubMenu");
....
case default:
//not sure on how to return nothing exactly
return null;
}
}
In your layout
#{Html.RenderAction("GetSubMenu","GenericControllerName");}
Create a Controller and Action to cater for this, I generally use something like NavigationController with a MainMenu action or similar.
In your action:
public ActionResult MainMenu()
{
return PartialView();
}
The from any where on your site layouts or views you can use:
#{ Html.RenderAction("MainMenu", "Navigation"); }
This also means that you can include any business logic in your action and pass a model to your MainMenu, maybe for checking roles etc.
Very handy.

pass values from view to a mehod in controller in mvc asp.net

I have 2 views and one method within Controller. This method will be called for each views.
I need to know how to write a condition within this method to determine whether view 1 or view 2 executed. is there any possible way that i can declare some variable or use viewstate/session (or something like that ) within the view1(viewstate=1) and view2(viewstate=2) and check that value of variable or viewstate within method in controller.
Thanks
I've got no idea what it's the actual context and the conditions that dictate which view should be rendered. How to pass the condition? Well, it depends on the condition itself, the condition must be based on something...where is this something coming from?
1- Coming from a business logic and or database...
public ActionResult ActionMethod()
{
bool condition = BusinessLogicFactory.GetCondition();
if(condition)
return View("View1");
else
return View("View2");
}
2- Provided in the request context...
public ActionResult ActionMethod(bool condition)
{
if(condition)
return View("View1");
else
return View("View2");
}
Try avoid session if not needed

mvc3 razor view from different controller

so in my profile controller page.
I have a method call create
inside the create method
if (Convert.ToInt32(calBMI) >= 25)
{
return View("Index", Survey);
}
I want to render the page to index of survey(survey is another controller take care of surveys), how do i do it to get it works,thanks!!
return View("~/Views/Survey/Index.cshtml", objSurvey);
Assuming objSurvey is your model/ViewModel object and Survey/index view is strongly typed to the type of objSurvey Model/ViewModel
EDIT : As per the comment, If your view is not strongly typed, you can ignore the second parameter
public ActionResult GetSomeThing()
{
return View("~/Views/Survey/Index.cshtml");
}
If your intention is to share this view among multiple controllers, it should be in the /Views/Shared/ folder. There is a lack of good reasoning to use a view outside of either the controller folder or the shared folder.
It seems to me that you can just redirect to list of surveys (if that's your intent).
return RedirectToAction("Index", "Survey");

Is current action a ChildAction?

How can I determine if current action is a ChildAction or routed main action? Should I check the URL and compare to the action's name? That's not so nice, since it's dependent on routing patterns...
Or should I make two actions of the same name, put a ChildActionOnly on one of them and have separate logic (mainly returning View() or PartialView())? How will the overloads be differentiated?
Okay, from an other perspective: How to make it so, that if it's a ChildAction then return a PartialView, otherwise a full View?
You could use the IsChildAction property:
public ActionResult Index()
{
if (ControllerContext.IsChildAction)
{
// The Index action was invoked as child action using
// #Html.Action("index")
}
...
}

ASP.NET MVC2: Can I return the same PartialView from more than one Action?

I am using PartialView object for all the CRUD operations in my project.
I would like to return from more than one action (Create/Read/Edit) always the same PartialView that I have previously binded to the correct model object.
How can I achieve this?
Thanks
You can specify, which (Partial)View you want to return from an action like this:
return PartialView("ViewName", viewModel);
If you want to use the partial view from different controllers, then put it in the Views/Shared folder.
Just add this to the end of every action that you want the partial returned from
return PartialView("PartialViewName", yourModelName)

Resources