Best practice for a multipage/form mvc web app

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

提交回复
热议问题