How to display hybrid forms in ASP.NET MVC? - asp.net-mvc

I designing forms (not a SPA template) that does the CRUD operations to manage a user's preferences. On all forms I also need to show the latest list of user preferences on the right side of CRUD forms.
I wonder if the controller should have the standard Index, Edit, Insert, etc method, then in the Insert view call a child action to Index method to list the latest preferences? Or should I have a ViewModel that contains both the individual record to be inserted and a List with a hybrid form bind to the View Model?

This is a great question I face every week. I love the concept of MVVM, but I choose to tackle it with child actions.
I love child actions because
I can pass values to them from my Model and have them adapt
I can call them with JQuery .load() and pass a value from a dropdown or other user input.
Their versatility make child actions Partials my first choice for dependent behaviors.

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.

a good substitute for viewstate in MVC?

I have an edit form in MVC. It contains different fields and 3 different partial views which is used like usercontrols. The scenario in these partial view: they contain a list with edit and delete plus an add button that when these buttons click a dialog box pop ups which contains a few related fields.
My main question is that what is the best solution to temporary save the changes of the lists(like viewstates)?
I'm asking because the main edit page contains more fields and I want in the case that the save button in the form is pressed, the whole data can be saved in database!(the tables in partial views has foreign key of the table of the main page).
Thank you in advance!
The web is stateless in its nature. Instead of looking for a workaround on the whole ViewState thing, it's better to try and embrace the medium you're using.
If you have many controls rendered on the same page you can either:
Use HTML5 Local Storage and persist on the client before submitting the whole form. There are many frameworks that will help you persist a form on the client side like Sysyphus.js
Make use of asynchronous ajax calls if you need to persist data on user input before submitting the whole form. Client-side calls can be managed using jQuery's ajax() function with ease and you can make use of the ASP.NET Web API to build the end-point on the server.

struts 2 actions coupled to pages?

Problem
While developing struts2 application I was asked to implement two pages: the one listing current user's books and the other listing current user's CDs. So I decided to implement two struts2 actions: ListBooks and ListCDs, both of them having standard dispatch result type, pointing to corresponding JSP pages.
Later the client changed his mind and decided that both lists of books and CDs should reside on the same page. So how can I reuse my actions so that I wouldn't need to rewrite any classes.
My attempt
First approach was to use action chaining. So that both actions can contribute their properties to value stack. But then there is a problem: we establish some unnatural order for actions. If I chain ListBooks to ListCDs then invoking ListCDs on its own causes some erroneous behavior - as it will result in a page where both books and CDs should be listed.
Another way was to use action tags. But I don't like it either as it forces separation of my page into multiple JSP parts and generally feels like doing things not in struts2 style.
Questions
So how do I combine my actions to form single page in a "clean way"? The problem seems to be so simple.
Another question spawned by this problem is about struts2 design - do struts2 actions take too much responsibility? Apart from being "single units of work" they are also coupled to presentation level. Everytime I change the structure of my pages I have to think about redesigning my actions. Am I missing something or struts2 really has this type of design weakness?
Struts 2 actions only take too much responsibility if they're designed that way--they should act as a thin bridge between the web and business layers.
Each of the earlier actions should have just had a service to retrieve the items in question. The new action would have both services, and expose the results of the user's books and CDs to the view layer.
Of course they're coupled to the presentation level--they are what determines what's available to the view. In any action-based framework this is the case, whether it's the action itself, the model in a ModelDriven action, a Spring MVC controller returning a view, a Struts 1 action form, request attributes, etc.
The amount of work necessary to support the type of change you describe is almost nothing: move the service from one action to another, and either expose the additional results directly, or add them to the exposed model.

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.

Action with AuthorizeAttribute or Dynamic UI in MVC 2 - Best practice?

There's so much I'm enjoying with asp.net MVC 2...but sometimes I feel like I'm trying to find a needle lurking in that haystack by beating it with a stick.
So. I have a list of objects. I have a strongly-typed view, which lists those objects. I have data annotations coming back just lovely, and all is well and good.
Now I want to create an admin view of that very same list. Do I:
Create a new view, called AdminList, and put all the administrative goodies in there (CRUD!), in a new action in the same controller, and decorate both actions with [Authorize], or
Modify the existing view, so that it dynamically detects the role of the current user and enables/disables admin functionality appropriately, or
Some third option?
I'm looking for the cleanest, least brittle approach.
TIA!
I would recommend you externalizing common parts in reusable partials and in the main view depending on the user role enables/disables admin functionality. Also it's not the view responsibility to test the user roles, this should be done by the controller and include this information into the view model (like a boolean property ShouldDisplayAdminPanel) so that the view decides whether or not include partials.

Resources