C# MVC Create Form - asp.net-mvc

I am using C# MVC. I have created a forms before where all of the data needed is on one form. Once the user fills, it then goes to the controller which inserts the data into a database table.
How would I handle a situation where the fields are on 3 different pages. On the 3 page, I like to submit. How does the data presist?
Also is a session variable Ok to use for data that is shared with many pages?

You could use different views, or use one view with a tabbed UI, and use three partial views as the content for each tab as an alternative.
You can use session, but then you have to ensure they can complete the content within the 20 minute time limit; as long as a postback occurs, then that limit is reset.

If your doing a wizard style workflow you could just have a ViewModel for each page and as the user progresses through to each page store each ViewModel in a Session variable. Then upon complete you could grab all the Models out of Session then aggregate the values into your completed form state.
Another way would be, for each POST call service that persists the page data into a data store so as the user navigates through each page, the entity is populated and stored on your data base via some service.

Could just have one partial view to spit out one single ViewModel that has properties for all 3 pages, and then use jQuery wizard plugins (or maybe you're writing your own) to simple manipulate the DOM to show/hide each page as the NEXT/PREVIOUS buttons are clicked. It will all still be part of one single FORM. And then hitting submit would post back to your controller method, say with a $.ajax() POST with serialized JSON.

If you shared a view model between all 3 of the pages in the Wizard, you could deploy HiddenFor fields on the pages to retain information that is not visible on that page of the Wizard.
This would persist data fields entered on other forms throughout your wizard and does not rely on the Session to store the information.

Related

One view having multiple button in asp.net mvc

I am not able to understand the flow on this page
how they are doing in MVC http://demo.nopcommerce.com/onepagecheckout
till now i created only one button for one page (view ) or provide me some similar link or similar code so i can understand
i want to implement same in my application
Thanks in Advance
The page is using AJAX to achieve the effect. Let's go through how it works.
The page is divided up into four sections.
Billing Address
Payment Method
Payment Information
Confirm Order
Each section is treated separately and are likely rendered using partial views. Each section has it's own form. When the user fills out a section and then submits that section, the form is submitted to a particular action. The Billing Address section submits its form to /checkout/OpcSaveBilling, the Payment Method section submits its form to /checkout/OpcSavePaymentMethod and so on.
When these forms are submitted (asynchronously, remember), the server handles the business logic and the validation and returns a result in the form of JSON. The JSON describes what happened, i.e. the result of the validation (success or fail), any errors that occurred and also contains HTML that the page can use to redisplay that particular section.
How is this data being remembered? Sessions. When forms are successful in their submission, the form data is stored per user in the session data. This way the system knows each user's settings and also knows where they are up to in the process.
The final step, Confirm Order, doesn't bother sending any form data because the server already knows everything through the session information.

Asp.net mvc displaying results about a selection being made

I am porting an application from wpf to asp.net mvc.
In the wpf I have a view in which the user selects from a combobox the name of a client and then in some textboxes, next to the combobox, some specific information about the client will show up (email, address, etc). In wpf I fill all this information in an observablecollection that resides in memory and when the client changes, I retrieve the other ones from the collection so it won't fo through the database.
Is there any way in asp.net mvc I can do this ? Or every time the client changes i will fetch from the database the extra information ? (will be slow)
This controls is just to select the client (and the extra information to help the user) so it should be fast.
How would you do it ?
If you don't want to make a call back to the server for every selection, then you should look at a solution whereby you pre-fetch all the data that you need and hold it in a javascript object. Then you could write a javascript method that would fire on the selection DOM event.
Sounds like what you are looking for is Session. You can pull the list from the database once, store it in session, and then reference session as needed for each client's data. This recommendation is based on presumption that the list is not being modified elsewhere, and therefore, invalidating the version of the list you have stored in Session.
Using Session in ASP.NET - http://msdn.microsoft.com/en-us/library/ms178581.aspx

MVC design pattern - who loads view initially

This query is about MVC design pattern in general and not ASP.net MVC framework
I understand in MVC (desktop application):
User clicks something in view
this is passed on to controller to manage
controller makes some changes in Model
Model calls method on view which has the logic to refresh UI
Questions around these:
Q1) Can controller also modify View or Model only updates View?
Q2) When screen loads for the first time, there is no Model change. Then, who fetches data from model and populates view? View directly calls Model and populates itself OR controller gets data and passes to view method OR some dummy event is raised at Model which updates View?
From model-view-controller :
The user interacts with the user interface in some way. (for example, presses a mouse button).
The controller handles the input event from the user interface, often via a registered handler or callback and converts the event into appropriate user action, understandable for the model.
The controller notifies the model of the user action, possibly resulting in a change in the model's state. (For example, the controller updates the user's shopping cart.)
A view queries the model in order to generate an appropriate user interface (for example, the view lists the shopping cart's contents). The view gets its own data from the model. In some implementations, the controller may issue a general instruction to the view to render itself. In others, the view is automatically notified by the model of changes in state (Observer) that require a screen update.
The user interface waits for further user interactions, which restarts the control flow cycle.
In the web context, you always have a request from a web browser. So the logic works as described above but there is a wrapper around it:
Browser sends request to server and waits for reply
The four steps above. In this case, the UI is also an internal model in the server.
The render engine creates a new UI (a new HTML page) from the internal UI model
The server sends the new HTML document back to the browser
Browser renders result

How to persist a model across multiple requests in ASP.NET MVC 2

I'm building a web application that has a particular model representing some events. Users need to be able to add N number of people to a given event. Choosing people is handled by a partial view.
I'm trying to build a menu that displays when users click "add a person" to the event. Because the event hasn't been filled out completely yet, there is nothing in the database to persist between requests.
I also have validation logic on the event page.
My proposed solution is to add the form to search or add for people on the event form itself and have a submit button that sends the values that have been added back to the server, where I can store them in ViewData or Session.
Unfortunately, doing this flags the validation.
My second solution is to load a partial view responsible for loading the UI to add/search for a person. I could add a little code on the method in the controller that returns a partial view storing the existing data in a session variable or viewdata. Trouble is, I have to submit the form to do it--again tripping the validation!!!
I'm wondering if perhaps I chose the wrong tool to do this...because in webforms, there would probably be a postback and you would just perform an operation on that postback. I'd like to avoid rewriting the application in webforms and am wondering if there are ways I'm overlooking in ASP.NET MVC.
Thanks in advance for the ideas!
I would probably have the partial view send it's data to the main page (with javascript). That way there is only one post to the server and it is when all of the data the user needs to enter has been filled out. How are you displaying the partial view? Is it on the main page (in a div), or is it a separate pop-up window? Either way, you should be able to use javascript to store this data on the main page and post all of the data back at one time.
HTH

ASP MVC - confirm page when adding object to database

I'm making a simple CRUD app with ASP MVC and I want to have a confirm page when creating a new object and inserting it into the database.
The problem is that I'm having trouble passing the object between actions. I tried to save it in the session after it's created and then retrieving it when the user confirms, but I'm getting an InvalidOperationException when I try to insert it into the database ( I'm using Entity Framework )
I'm not even sure if I'm approaching this the right way. Any ideas?
What I like to do if the schema allows for it, is to have an active flag (and timestamp field) on the record. You insert on the first page without setting that flag. The confirm page merely sets the active flag. Another process can clean dead records that were not confirmed within a certain range of their timestamp. And the object or entity never ties up session memory.
edit for clarity: as a result you only pass the id of the created entity to the confirm page
Tim's is the best basic answer to this but if you don't want to include the extra logic you could also consider using hidden fields on the confirm page so confirming actually resubmits the form data (this means less DB trips and means that you don't have old unconfirmed entries sitting in the DB to filter out but means more data to and from the client).
Another alternative that might be preferred from a UI point of view is to have Tim's answer but if JavaScript is enabled make the submit button instead pop up a confirm screen, clicking OK would submit both the form and the confirmation in one go.

Resources