Loading multiple views from controller action - asp.net-mvc-3

I have a page which has 5 tabs. I need to load values in all the three tabs when i click on search in the main page. The three tabs has content from 3 partial views. I mean to say how can I update all the three partial views from the search controller action.
Regards,
Amit Agarwal

I never went to school and only learned how to program a short time ago but I think what might help you is to use a control structure to "update" the models of the 3 partial views, then incorporating those models in the partial views.
I think there is a lot of different ways you can do this, I just hope my example is logical as it is probably not industry standard or anything like that.
Partial View Controller section
Logic search controller action preformed or not?
If preformed do this stuff.
- $partial_model1 = the appropriate model
$partial_model2 = the appropriate model
$partial_model3 = the appropriate model
else do other stuff, for example set default partial models for partial views.
End Logic search controller action preformed or not?
End Partial View Controller section
Tab# Controller section
Tab1
$tab1_model = the appropriate model for Tab1
Load Tab view 1
inside Tab view 1 incorporate $tab1_model, load Partial view 1 incorporate $partial_model1, Load Partial view 2 incorporate $partial_model2, Load Partial view 3 incorporate $partial_model3
Tab2
$tab2_model = the appropriate model for Tab2
Load Tab view 2
inside Tab view 2 incorporate $tab2_model, load Partial view 1 incorporate $partial_model1, Load Partial view 2 incorporate $partial_model2, Load Partial view 3 incorporate $partial_model3
Tab3
$tab3_model = the appropriate model for Tab3
Load Tab view 3
inside Tab view 3 incorporate $tab3_model, load Partial view 1 incorporate $partial_model1, Load Partial view 2 incorporate $partial_model2, Load Partial view 3 incorporate $partial_model3
Tab4
$tab4_model = the appropriate model for Tab4
Load Tab view 4
Tab5
$tab5_model = the appropriate model for Tab5
Load Tab view 5
End Tab# Controller Section
I could have loaded the views in the controller section instead but by the use of your term "partial views" I feel you might want to embed those "partial views" within your Tab# views.

Related

MVC 3 Combinging multiple controls in one sort control template

I'm working on some forms, and often I can reuse the same amount and combination of fields together, is it possible to group them all as a template and then call it from the page?
For example:
2 radio buttons with labels and 2 texboxes under.
How is it called so i can do a proper research?
You can use Partial View, its like a reusable component.
These links will elaborate more:
http://www.devcurry.com/2012/04/partial-views-in-aspnet-mvc-3.html
http://www.dotnet-tricks.com/Tutorial/mvc/2IKW160912-Partial-View-in-Asp.net-MVC3-Razor.html
MVC3 Partial Views
Create a Partial View and keep your markup which you want to reuse in that. You can use these Partial views in other views as needed then.
You can use the Html.Partial helper method to call the partial views in other views.
#Html.Partial("RecentItems")
You can also pass some model to your partial view as well
#Html.Partial("RecentItems", Model.RecentItems)
Assuming Your partial view is strongly typed to a class which is of the same type as the type of Model.RecentItems, of the caller view.

Render Specific Section of Views in ASP.Net Mvc 4

In Asp.Net Mvc when you Render view in a layout that defines some Sections, all Sections in view placed in proper Sections as you defined in layout file,
consider a situation when you want to RenderPartial a view (which have some Sections) in another view, but all the code which you placed in Section of child view does not render in parent view (because the parent view does not necessarily have the defined Sections) so only parts of view that does not placed in any Section are rendered,
Now my question: is it possible to render specific Section of view with methods RenderPartial or RenderAction ?
You can put you code in separate view. And you will be able to render this part with RenderPartial. If you create action in controll you will ba able to use RenderAction .
But in your code you can define section like
#section MySection{
#Html.RenderPartial(/*bla bla*/)
}

Mutually exclusive search options on MVC 3 view

I'm working up a view that will use 2 sets of search options and would like to know if there is better way than I'm headed.
on right hand side it will search by username, lastname
on left hand side it will search by an entity type or entity name
the results should be returned as a grid below the search.
I've considered that I may need 2 forms on the view but not sure if that is the right direction. Would 2 partial views each with their own form be better? If so, how would data be returned to the main view?
I'm trying to keep this simple by posting to controller actions and returning views instead of a bunch of confusing jquery.
Currently I have a model with 2 sub-models that each define the search fields that will allow a user to enter data into the textboxes.
What is the proper way to handle this in MVC 3?
You can use two partial views if you are going to re-use the forms on other views or if you just want to encapsulate the view code. I would suggest two forms that post to different controller actions 'SearchPerson(model), SearchEntity(model). Both actions can return a common 'Results' view model to a 'SearchResults' view.

Finding, loading and adding partial views dynamically

I've been tasked with converting an existing webforms application to mvc 3 razor.
The application currently has an aspx page which has a static header user control and "n" amount of other user controls which are dynamically created. In the code behind for the file, it is executing the below code in various specific sections to dynamically process user controls with information provided from the database.
I know how to statically create partial views, but being somewhat new to MVC, how would I go about defining this new "aspx" page and also to dynamically find, load and add the partial views (each equivalent to the below webforms code)?
btw, the code will be in C# as well.
Dim parent As Control = Page.FindControl(_moduleSettings.PaneName)
Dim portalModule As PortalModuleControl = CType(Page.LoadControl(_moduleSettings.DesktopSrc), PortalModuleControl)
parent.Controls.Add(portalModule)
I think I can do something like this when the page is rendering. I want to make it as simple as possible.
The "PaneName" will be set in the parent variable which determines where in the page it will be shown (Left, Right, or Main)
The "DeskTopSrc" is the name of the partial view to display.
So, take the code out of the code behind and place it in the main View. Perform the above processing logic in the View (boy, switching from aspx code behind to a View throws me a loop. I gotta get use to doing the processing in the View. Reminds me of Classic ASP, but the Razor syntax will help).
Display the partial view via the #Html.PartialView('partial view name'). This view might have a grid in it associated with a specific model.
Below is the part I am unsure about.
I've done database processing for a main View associated with a Controller, but not with a partial view that needs to do some database processing.
Perform any database processing logic (if any) for this partial view in the Controller associated with the main View (which contains this partial View).
In the Action Index method while looping over these "partial views", I can get the data and display the views....
Ahhh, I think I got it.....
After carefully thinking it through, if someone could help me out with the last statement here, I would greatly appreciate it.
1.Have partial views already statically created with the specific HTML markup that I need in the Views/Shared folder.
2.In the main View, I will already have
#Html.Partial(ViewData["partial_view_left"])
#Html.Partial(ViewData["partial_view_right"])
#Html.Partial(ViewData ["partial_view_main"])
statements in specific locations of the HTML which will render the partial views as I retrieve their names from the database.
3.In the Controller's Index method, I need to do the following:
a) Loop through the converted logic (from the CodeBehind of the existing WebForms page in the PageLoad event) in the Index action method of the new Controller which will load the partial views dynamically.
1) Find out where the partial view will be displayed (left, right, main) from the database via the "parent" variable.
2) Find out the name of the partial view that will be displayed from the database via the “DesktopSrc” variable.
eg: ViewData["partial_view_left"] = "left_view"; OR
ViewData["partial_view_right"] = "right_view"; OR
ViewData["partial_view_main"] = "main_view";
3) Right here is where I am unsure of how to properly display the partial view.
I need to have the equivalent of a webforms "Controls.Add" method to render each partial view from the Controller that I retrieve from
the database from step 3.a.2
What statement can I use in this Index method of the Controller that will accomplish this?
In other words, if I dynamically need to display several partial views inside of a parent view, how is this accomplished in MVC?
I know for each partial view, I can send over the model associated with it, but I just don't know how I can place several partial views inside the main view page at run time from one Action method.
If your partial views need to do some processing, like database retrieval, then you should use
#{Html.RenderAction("ActionName");}
This will call an action method (which doesn't have to be on the same controller) that can dynamically choose a view based on logic, and populate the ViewModel with data from the database.
public ActionResult ActionName()
{
var modelData = GetData();
return View(settings.DesktopSrc, modelData);
}

MVC3 : How to call A view from another controller from a different controller

I have to make a form consisting of several sub forms which may or may not contain grids.
For simplicity lets say i am using 2 models
1. Applicant Detail : where the applicants basic information (Name, DOB, Gender) is placed.
2. Applicant Experience : where the applicants prev work experience details are being placed.
Now for organizing purpose i am thinking of making two different controllers Namely ApplicantDetail and ApplicantExperience and both will contain their respective functionalities. But as soon as the user enters his basic information i want the same view to call in the index view of ApplicantDetail which will have a grid and the user can enter his/her prev working details in the form of rows.
My basic question is, is there anyway that can i create single razor view which will contain the razor view of my ApplicantDetail as well??
I believe what you are looking for is RenderAction.
Using RenderAction you can invoke controller/view within any view.
So if you had some razor view and second Controller and View combination, you could invoke it like so within the first view:
#{ Html.RenderAction("Action", "Controller2"); }
This can be useful when trying to modularize functionality.
See this blog post from Phil Haack for more details:
Html.RenderAction and Html.Action

Resources