在其他问题中,我已经阅读了很多关于控制器上的创建操作的内容,其中最佳做法是对GET方法执行RedirectToAction,但是如果我想显示成功保存的消息并清除模型或者提交提交并创建新的输入类型。例如,这是我的代码,是否有更好的标准兼容方式来清除创建模型?
[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);
}
答案 0 :(得分:1)
你可以试试这个:
[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);
}