在其他問題中,我已閱讀了許多有關Controller上的Create動作的信息,其中最佳做法是對GET方法執行RedirectToAction,但如果我想要顯示成功保存的消息並清除模型或提交提交併創建新的輸入類型。例如,這裏是我的代碼,是否有更好的標準兼容方式來清除創建模型?在ASP MVC中清除模型的最佳方法創建動作
[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);
}
你可以用'TempData'的留言,然後檢索它在GET方法時您重定向。 –