Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for a multipage/form mvc web app

I have a project using ASP.Net MVC & EF5. The Model has a large enough number of fields that the app will need several pages to collect all of the data.

Once the data is collected it is submitted to a Web Service/WebAPI. (This can not be changed or I would use EF entities instead).

What is the best practive or best suggestion for persisting the data model from page to page as it is filled?

like image 630
John S Avatar asked Jun 18 '13 20:06

John S


People also ask

Can you mix webforms and MVC?

Luckily, the answer is yes. Combining ASP.NET Webforms and ASP.NET MVC in one application is possible—in fact, it is quite easy. The reason for this is that the ASP.NET MVC framework has been built on top of ASP.NET.

Which is better Web forms or MVC?

More Control-The ASP.NET MVC framework provides more control over the HTML , JavaScript and CSS than the traditional Web Forms. Testability-ASP.NET MVC framework provides better testability of the Web Application and good support for the test driven development too.

Is MVC faster than web forms?

Show activity on this post. My completely unscientific opinion: Yes; ASP.NET MVC is faster than web forms. ASP.NET MVC gives screen pops on the order of 1 to 2 seconds. Web forms is more like 3 to 5 seconds.


2 Answers

You could create one ViewModel class that each page uses (strongly typed view) and just store the fields that aren't being edited in hidden fields. Your controller would have an action method and view for each step. When each step is submitted you return the View (along with the View Model) for the next step.

On the last page, you submit the View Model and save it.

Contoller code:

[HttpPost]
public ActionResult Step1(MyBigViewModel model)
{
     //do work
     return View("Step2", model);
}

[HttpPost]    
public ActionResult Step2(MyBigViewModel model)
{
     //do work
     return View("Step3", model);
}

[HttpPost]
public ActionResult Step3(MyBigViewModel model)
{
     //save here
     return View("Success", model);
}

Another option is to use a "Wizard-like" UI on a single page. Here's one I've used before with success:

https://github.com/mstratman/jQuery-Smart-Wizard

like image 187
Rob Avatar answered Nov 04 '22 13:11

Rob


Question - 1

Why can't I use the Domain Model directly interact with View or Why can't I Create a View Model and pass all Properties of Domain-Model?

Answer

Let's say you have 50 fields in your class. I have implemented Data Annotations so Required Fields are also present. ok. I am in Step-1. I submitted the form. My Post Action Method said, Form can't be submitted !!!! Why???

It's because there are some Required Fields which are not the Part of Step 1. I have a Question from you. Will you like to keep all Properties as mention in other answer? If you want to add all the properties in one View-Model then why one interact with Domain-Model directly as per the suggestion provided in other answer? So, adding all properties in one View-Model will be worst. right?

Question - 2

What is the best practive or best suggestion for persisting the data model from page to page as it is filled?

Answer

1. Use View Models with necessary properties only(that are required for 
   particular Step.). So there can be many View Models on the basis of 
   your Steps. This process will be very useful in long run 
2. Use AutoMapper to populate the info required for View Model from Domain Model.

Using Strongly Types View Models, once the data is sent to Post Action Method after then As per my knowledge you can use TempData to store the posted data. It is like a DataReader Class, once read, Data will be lost. So that stored data in TempData will become null after read.

var Value = TempData["keyName"] //Once read data will be lost

Question - 3

The Model has a large enough number of fields that the app will need several pages to collect all of the data.

Answer

So to persist the data even after the data is read you can Alive it like below

var Value = TempData["keyName"];
TempData.Keep();                   //Data will not be lost for all Keys
TempData.Keep("keyName");          //Data will not be lost for this Key

Question - 4

How will you handle the case when you have both Next and Previous Buttons?

Answer

TempData works in new Tabs/Windows also, like Session variable does.

You could use Session Variable too you are able to keep the data across Controllers/Area as well

Hope this post will help you alot.

like image 22
13 revs, 2 users 99% Avatar answered Nov 04 '22 13:11

13 revs, 2 users 99%