MVC partial views, partial model, partial controller? - asp.net-mvc

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.

Related

Developing web applications with widgets (or re-usable components) in ASP.NET MVC

I have experience in writing code... but new to developing web applications.
I am in process of choosing a framework for my project. Based on my initial research almost everything is very VIEW centric where after all business logic has been executed a model is populated with data and passed on to the VIEW.
So the lowest level of granularity is the VIEW.
But I wonder what is the right technology to use if I wanted to develop re-usable widgets or controls. and then reuse them across multiple VIEWS.
i would prefer if the controls are in JavaScript and then they can easily be reused across pages. (so no asp.net server controls or web forms).
So if I were to select ASP.NET MVC 4 ... does it have anything to help me write code in reusable widgets... or will it simply ask me to write the VIEWS which work with the data provided by the model?
Sorry if this is newbie question
to eloborate on what I mean by a widget.
Suppose I am writing a discussion forum web appliction. I want to write a widget called post which has 3 views. View 1 is edit mode. View 2 is summary mode. view 3 is Detail mode.
I throw in this widget in a page called QuestionStream and in this page the widget appears in summary view. I perform data binding so that i get a list of questions.
I throw in this widget in a page called ThreadView and in this page the widget appear in detailed view. I perform data binding and I get all the details of the question.
I throw in this widget in a page called NewQuestion and in this page the widget appears in edit view.
So its a self contained control... but is reused in multiple places in different modes (so to speak).
Here is summary list on how to create reusable components/pages/widgets in MVC. There are many more ways, these are just examples to give you starting point on what to look for or how to make one.
1. Partial Views
Typically used for breaking down large views, into smaller views then combined at runtime
View-centric approach, the controller can choose a View to generate.
Tutorial
2. Render Action / Render Partial
Invokes an child ActionMethod directly in the view page as inline
expression.
Useful for partial views that requires a business logic.
3. Custom DisplayFor/EditorFor Templates
Model-centric, which means they take model metadata (Attributes/Annotations).
Cool tutorial
4. Custom HTML Helpers
"Commonly used to generate boilerplate HTML" (LINK)
The following link shows how to create page "widgets" in MVC. The author doesn't mention "widgets", but it is the same as you are looking for.
http://www.mikesdotnetting.com/Article/105/ASP.NET-MVC-Partial-Views-and-Strongly-Typed-Custom-ViewModels
Based on the tutorial mentioned, below is the image on what it should look like (notice the "Most Popular" section, that widget is completely reusable across all pages)
ASP.NET MVC 4 has the ability to write methods that are re-usable. The View is expressed in mark-up (typically) and will call methods from the Controller to populate the View.
To be honest though, even though you're asking a very simple question, I think what you're trying to get at is the re-usability. All the technologies you listed: server controls, web forms and mvc4 have that capability. It all depends what you want to do.

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 create composite views in ASP.Net MVC 3?

How do I create a view in ASP.Net MVC 3 that defers creation/rendering of a portion of the view to another view/controller, without knowing at design time what view/controller will be deferred to?
I have a Dashboard view/controller that uses a Layout page. the dashboard view does most of the work of rendering the overall screen. However a certain placeholder section of the overall screen will differ based on business rules, configuration, user interaction etc. There are three separate plugin views/controllers that may need to be rendered in the placeholder section. How do I instruct the Dashboard view to render a specific plugin view once the Dashboard controller has determined which plugin should be visible. I have accomplished this already by putting flags in the Dashboard model and then having conditional logic in the Dashboard view call Html.RenderAction with the appropriate parameters based on the flags. This seems unsatisfactory and I would assume there is already a simple built in functionality for this kind of scenario, please advise.
Thank you
You are really asking "how do I dynamically render partial views who's identity is known at design time."
One can get there two ways, either through Html.RenderPartial or Html.RenderAction. And both have their place. If the main page "knows" enough to have the view data, then RenderPartial is your the better bet as it is a bit more efficient than RenderAction. If you only know, say, a unique identifier for your portal widget than RenderAction works because you've now got a fully capable controller method that can do things like go back to a database and render a view.
You could write your own htmlhelper and use that in your view instead of using the flags.

How do I display multiple pieces of user information on every page in ASP.NET MVC 2?

I'm building a site in ASP.NET MVC 2 that will allow for anonymous users and registered users. When a user is logged in, I want to display multiple pieces of information related to that profile on every page (i.e. hometown, favorite color, etc.). From a view perspective, I understand using Master pages and creating partials to keep it DRY.
However, where I am getting stuck is how do I pass this user information to the view for every page? I already have the relationships between database tables established (I'm using EF), so I can do this on an individual basis for each action through ViewData, but that's obviously ridiculous for every page on the site.
So far, my research has started to lead me down the path of creating a base controller and base view model that the other controllers and view models will inherit from. But I feel like I'm missing something obvious. Any pointers?
If you have your Master page use the RenderAction method, it can invoke controller actions for the various repetitive parts of your page, each of which can perform data access and render a partial view. That allows you to separate your view models while still displaying certain elements on all your pages.
This approach works great for us.
We use a base controller to store it in ViewData.
You could also use an action attribute on the controller rather than inheriting from a base controller.
You could create a base class for your models that contains the data that is display on every page.

What is the proper structure for Asp.net MVC?

I read the Pro .Net Asp.net MCV book over the weekend and it provides some good examples on setting it up and using it. However my question is what is the structure of an MVC project should be. I ran into problems once I started trying to transfer control from one controller to another. It seems that you can have multiple views within one controller. Also when you execute the Redirect("Action", "Controller") command it seems that the routing wants to look for the view within a sub of that controller. So my questions are:
Is there rule of thumb of 1 controller to 1 view?
Should you call another controller from a controller?
What is the proper way to transfer control from one controller to another?
You can have as many views/partial views per controller. The rule of thumb as far as one can deduce it from the MVC samples is, that a controller encapsulates a set of functionality that belongs together, e.g. listing products and creating, updating, deleting as single product.
You can use Html.ActionLink to route from one view to another. To call one controller from another, IMHO, makes only sense for partial views - however that depends on the problem.
Html.ActionLink or Html.RouteLink.

Resources