Best practice for a multipage/form mvc web app

后端 未结 3 1680
没有蜡笔的小新
没有蜡笔的小新 2021-02-09 03:28

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 da

相关标签:
3条回答
  • 2021-02-09 03:45

    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

    0 讨论(0)
  • 2021-02-09 03:52

    I actually came across this article , which was very informative and helped me : https://blacklineit.com/2015/10/14/rolling-your-own-viewstate-in-asp-net-mvc/

    Basically the article comes down to this : "The easiest and cleanest way I found to persist data between pages is to use the great Newtonsoft JSON library to serialize viewmodel into a single hidden field, and deserialize on POST requests. "

    Read it to get the background and additional info.

    0 讨论(0)
  • 2021-02-09 04:03

    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.

    0 讨论(0)
提交回复
热议问题