Multiple Views in the same page MVC ASP.NET - asp.net-mvc

I want to display different Views on the same page in ASP.NET MVC technology . How can it be acheived.
I have data coming in from 2 different Tables and for that i have 2 different Views for display. How can I display both these views on one single page.
Is there a concept of a View within a view in MVC ? Or is there some provision for making a master view which can encapsualte multiple views. ?

These link's will help you (first gain knowledge of Partial view)
http://www.dotnet-tricks.com/Tutorial/mvc/2IKW160912-Partial-View-in-Asp.net-MVC3-Razor.html
http://www.codeproject.com/Articles/351867/Basics-of-ASP-NET-MVC3-Part-II
http://www.c-sharpcorner.com/UploadFile/aravindbenator/mvc-3-razor-and-partial-view/

ASP.NET MVC supports the ability to define "partial view" templates that can be used to encapsulate view rendering logic for a sub-portion of a page. "Partials" provide a useful way to define view rendering logic once, and then re-use it in multiple places across an application.
Refer:
Re-use UI Using Master Pages and Partials
Creating Partial View

Yes, the aspx webforms has the Master page and then you use user controls .ascx files.
With MVC, the "master page" is by default under
Views\Shared\_Layout.cshtml
Now you can also end up using the wonderful Twitter bootstrap and really start to make greater looking layouts with easy to use css etc..
Your controller can return
PartialViewResult
as mentioned by the other answers from the others, those links should get you on your way.

Related

MVC partial views, partial model, partial controller?

Currently i am investigating if MVC is the way to go for the new major version of our web application. We have an existing web application with webparts, dynamically rendered according to some user settings. Each webpart has its own data and own logic (for example, one webpart with user information, one webpart with currently logged-in users, one webpart with appointments etc. etc.).
What we need to accomplish (i think) is to render a single view, which contains several partial views. Each partial view represents a different model, and has its own logic.
I figured out how to put multiple partial views within a single view, but i don't know how to handle the business logic for each view (in "partial controllers"? if possible at al?), and handle the model for each view?
So the main purpose is to render a page with multiple dynamic views (according to what the user has configured), each with its own logic and data. And then when, for example, a button is clicked in a partial view, the corresponding controller is called to handle the event, and returns the updated partial view. The partial views need to be loosely coupled, and updated async.
From what i've seen so far the most tutorials and documentation are focussing on MVC in general, not on how to separete the business logic and model for eachr partial view.
So I'm not asking how to do this, but:
Is it possible to easy accomplish this with MVC 4 or 5?
Does anybody know a good real-life example or tutorial about this?
I hope anyone can point me in the right direction or share some thoughts on this...
You could make one or more controllers with an action for each webpart.
Group all related webparts in the same controller but make an action and View+ViewModel for each webpart. Then use the Html.RenderAction method to call these actions (and have your webparts placed) on your page/main view.
DISCLAIMER: This said, each call to Html.RenderAction creates a complete mvc flow, instanciating a controller, model and view and finally renders the whole thing and passes the value to your page/main view. Having lots of Html.RenderAction has the potential to slow your page creation a lot. You could look into DI/IoC like Unity and consider reusing controllers, or just look into System.Web.Mvc.DependencyResolver to handle the creation of controllers yourself.

User Control Equivalent in ASP.NET MVC 4

I'm working on a new ASP.NET MVC 4 app. I'm really trying to do things the "MVC" way. In particular, I'm interested in using the Razor view engine. Please note that I come from a web forms background, so my questions may seem awkward.
I'm trying to learn if there is something equivalent to user controls in MVC? If so, what are they called? If not, what is the recommended way to creating a reusable component in MVC?
You're looking for #Html.Action and #Html.Partial.
They allow you to either render a partial view or a full blown controller action in a view. This is a common pattern of reuse in MVC land.
Occasionally you would want to make displayFor or editorFor templates, if a controller action is too heavy. The rule of thumb is if you need to do it multiple times on the page and it needs to be posted back in a form, think about doing it in a template.
Controls in asp.net cover a rather large swath which MVC granularizes a bit more.
To creating a reusable HTML component in MVC you can create a partial view in the Views/Shared folder and use #HTML.Partial("_PartialViewName") to include it in any other view or partial view. You can find out more about partial views in this article.
You could try:
Add a View Start _ViewStart.cshtml in Shared folder
Make Layout Null (if required) #{ Layout = null; }
Call this page where you want like #Html.Partial("_ViewStart")

MVC replacement for web form user controls (ASCX)

I'm quiet newbie to ASP.NET MVC world. In Web Form we can write a user control and encapsulate all the details within. We can then reuse the control on N number of pages which is a good code-reuse.
I would like to do the same in MVC 3/4 and haven't had any lucks. Could someone please help me with how above can be achieved?
Thank you.
Probably the closest thing to an ASCX in MVC is a RenderAction.
Similarly to a user control, you can have your partial view binded to a specific action that is independent from the container view and it's action.
There is no exact equivilent. There are, however, ways to reuse code. You can create HtmlHelper extensions (like the Html.Whatever() methods), Or you can use Partial Views, or you can use templates. All of these do different things, but they offer various ways to reuse code.

ASP.NET MVC Custom Controls

Is it safe to say that custom controls with ASP.NET MVC are most times just partial views? And if that's the case, I'm guessing it's always up to the implementing application to dictate the behavior (through controller code) of these controls?
I have done a bit of searching, and there is almost no resources on ASP.NET MVC custom controls (either that, or I'm missing the mark with my Google skills).
Partial views are more a template for either a control or a set of controls that can be shared between views.
I'm not sure they are "Custom Controls" as such and it sounds like you are coming from an ASP viewpoint.
I think you need to first get in the mindset of MVC and out of ASP.
So for a list of items you may have a partial view that takes the list of items and a partial view that takes an actual item. So "pvCustomerList<List<customer>>" and "pvCustomer<customer>".
The pcCustomerList iterates through the list and creates a pvCustomer for each customer in the list.
Partial Views don't really have code in the controller. Instead they are passed data from the view. If there is a submit action in the partial view, then this is either handled by the controller for the view or a jQuery post back.
I hope this clears things up a little for you.
Have you checked out NerdDinner sample?
There is nice article on asp.net mvc website regarding form helpers (helper methods for views). there is explained how to create custom helper method
Link is http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs

How do I generate content once and reuse it across all pages in ASP.NET MVC?

In my ASP.NET MVC site all pages share the same content (header & top menu).
What are my options to generate that content is only one place and reuse it in all pages. I also want the link the user is currently on to be handled appropriately (not show up as a link).
In MVC3 this is usually done in the layout page: http://weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx
In my ASP.NET MVC 2 application, I use a Master Page as suggested by Rich. I just wanted to add that I use a Master Model that all of my view models inherit from to return dynamic data that is needed in the master page (i.e. something to help you determine the active link to highlight). Every time I render a view I explicitly pass it a view model so those values from the master model are always available.
That master model gets its data from things that are globally available (i.e. my custom identity, http context properties, etc.) - because I don't want to manually initialize those properties every time I render a view (which defeats my goal of thin controllers).
You can also use ASP.NET Master pages. http://msdn.microsoft.com/en-us/library/wtxbf3hh.aspx
If you start a new ASP MVC project in Visual Studio I believe it'll give you a masterpage in /Views/Shared
If it's dynamic data; then you can always use a RenderAction() inside of a Masterpage. Is this dynamically generated data?

Resources