Reputation: 31
I have a multi-page form to submit and update an application for an event built using ASP.NET Core MVC. I want it to use the POST-Redirect-GET pattern, but without losing the input fields contents and the validation errors. Each page of the form consists of the display information managed by the organizer (1) and the information entered by the user (2) (e.g. a list of locations of venues (1) the the choice of venues (2)). I have a working implementation, but I'm not sure about performance. I can best explain my approach using (C#) code:
using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
public class ApplicationController : Controller
{
[HttpGet]
public async Task<IActionResult> Step1(int id)
{
this.TempData.TryGetValue("viewModel", out Object? viewModelJSON);
Step1ViewModel viewModel;
if (viewModelJSON == null)
{
// create empty view model
viewModel = new Step1ViewModel();
viewModel.ID = id;
// when simple GET: show data from db
var userInputModel = await this.LoadVenueChoicesAsync(id);
// fill view model if data exists
if (userInputModel != null) {
viewModel.UserInput = BuildUserInputViewModel(userInputModel);
}
}
else
{
// when GET after POST: show input
viewModel = JsonSerializer.Deserialize<Step1ViewModel>(viewModelJSON);
await this.TryValidateModelAsync(viewModel);
}
var venueList = await this.LoadVenuesAsync();
viewModel.Information = BuildInformationViewModel(venueList);
return this.View(viewModel);
}
[HttpPost]
public async Task<IActionResult> Step1(Step1ViewModel viewModel)
{
if (this.ModelState.IsValid == false)
{
var viewModelJSON = JsonSerializer.Serialize<Step1ViewModel>(viewModel);
this.TempData["viewModel"] = viewModelJSON;
return this.RedirectToAction("Step1", new { id = viewModel.ID });
}
else
{
await this.StoreVenueChoicesAsync(viewModel.UserInput);
return this.RedirectToAction("Step2", new { id = viewModel.ID });
}
}
}
The web form is going to be used for a short time by many users, so performance must be good. Because of that I would like to validate this approach. Some specific questions I have:
Upvotes: 2
Views: 180