We are building MVC multi-step form that will have 4 steps:- Contact-details, Car-details, Car-images, Car-condition. At each step, we ask few car related details from the user like this:
We have to save each step information to the database and we have made separate MVC controller for each step, for example:
[HttpGet]
[Route("contact-details/")]
public ActionResult ContactDetails()
{
return View("~/Views/ContactDetails.cshtml");
}
[HttpPost]
[Route("contact-details/")]
public ActionResult ContactDetails(ListingDetails listingDetails)
{
if (ModelState.IsValid)
//extract ContactDetails from ListingDetails and save in db
return View("~/Views/CarDetails.cshtml");
return View("~/Views/ContactDetails.cshtml");
}
[HttpPost]
[Route("car-details/")]
public ActionResult CorDetails(ListingDetails listingDetails)
{
if (ModelState.IsValid)
//extract CorDetails from ListingDetails and save in db
return View("~/Views/CarImages.cshtml");
return View("~/Views/CarDetails.cshtml");
}
[HttpPost]
[Route("car-images/")]
public ActionResult CarImages(ListingDetails listingDetails)
{
if (ModelState.IsValid)
//extract CarImages from ListingDetails and save in db
return View("~/Views/CarConditions.cshtml");
return View("~/Views/CarImages.cshtml");
}
[HttpPost]
[Route("car-condition/")]
public ActionResult CarCondition(ListingDetails listingDetails)
{
if (ModelState.IsValid)
//extract CarCondition from ListingDetails and save in db
return View("~/Views/ThankYou.cshtml");
return View("~/Views/CarConditions.cshtml");
}
The ListingDetails has following structure:
class ListingDetails
{
public ContactDetails contactDetails;
public CarDetails carDetails;
public CarImages carImages;
public CarConditions carConditions;
}
At next click of Contact-details form, we will send only step1 data. At next click of Car-details form, we will send step1+step2 data. At next click of Car-images form, we will send step1+step2+step3 data. At next click of Car-conditions form, we will send step1+step2+step3+step4 data.
We are sending all previous steps data along with each next click is because of the top navigation. For example user may fill step1, click next=> fill step2, click next, from step3 comes back to step1 and edit something. Now, user go to step3 again through top navigation. So, for this case, we will have to send complete data filled till now with each request.
Can we somehow avoid sending all previous step data with each step?
Is this correct way to implement multi-step form in MVC?