Reputation: 915
In other questions I have read many things about the Create action on a Controller, where the best practice is to perform an RedirectToAction to the GET method, but what if I want to show a succesfully saved message and clear the model or a submit a submit and create new input type. For example here is my code, is there a better standard compliant way of clearing the create model?
[HttpPost]
public ActionResult Create(RegisterUserViewModel model)
{
if (ModelState.IsValid)
{
/* perform create action validation and operation in service and repository layers */
var service = new UserManagementService();
var registerUser = service.RegisterUser(model);
if(registerUser == ServiceStatus.Successful)
{
//Here I set the "Correctly created" message to the model
model.ServiceResponse = registerUser;
//Here I "clear" my model
ModelState.Clear();
model = new RegisterUserViewModel();
//What is the most useful suggestion is this, but here I can´t pass model data
//return RedirectToAction("Create");
}
}
return View(model);
}
Upvotes: 1
Views: 166
Reputation: 3329
You can try this:
[HttpPost]
public ActionResult Create(RegisterUserViewModel model)
{
if (ModelState.IsValid)
{
/* perform create action validation and operation in service and repository layers */
var service = new UserManagementService();
var registerUser = service.RegisterUser(model);
if(registerUser == ServiceStatus.Successful)
{
//set succesfully saved message
TempData["Message"] = "Success Message";
return RedirectToAction("Create");
}
}
return View(model);
}
Upvotes: 1