Best practice for a multipage/form mvc web app

后端 未结 3 1682
没有蜡笔的小新
没有蜡笔的小新 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 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.

提交回复
热议问题