Initial revaluation of the MVC3 model when redirecting after a state / state change

Given that web applications should always be redirected after POST (or any non-repeating server-side state change request) ...

... how do people use MVC3 Model Validation and perform mandatory redirects?

+6
source share
2 answers

Usually you redirect only after successful publication (without model validation errors), otherwise you send a page with a validation error message.

MvcContrib template redirection project code: ModelStateToTempDataAttribute

This was also mentioned along with other tips in the article "Best Practices" on weblogs.asp.net (the author seems to have moved the blog, but I could not find the article on the new blog). From the article:

One of the problems with this pattern is that the check fails or any exception occurs, you need to copy ModelState to TempData. if you do it manually, please stop it, you can do it automatically with action filters, for example:

controller

 [AcceptVerbs(HttpVerbs.Get), OutputCache(CacheProfile = "Dashboard"), StoryListFilter, ImportModelStateFromTempData] public ActionResult Dashboard(string userName, StoryListTab tab, OrderBy orderBy, int? page) { //Other Codes return View(); } [AcceptVerbs(HttpVerbs.Post), ExportModelStateToTempData] public ActionResult Submit(string userName, string url) { if (ValidateSubmit(url)) { try { _storyService.Submit(userName, url); } catch (Exception e) { ModelState.AddModelError(ModelStateException, e); } } return Redirect(Url.Dashboard()); } 

Action filters

 public abstract class ModelStateTempDataTransfer : ActionFilterAttribute { protected static readonly string Key = typeof(ModelStateTempDataTransfer).FullName; } public class ExportModelStateToTempData : ModelStateTempDataTransfer { public override void OnActionExecuted(ActionExecutedContext filterContext) { //Only export when ModelState is not valid if (!filterContext.Controller.ViewData.ModelState.IsValid) { //Export if we are redirecting if ((filterContext.Result is RedirectResult) || (filterContext.Result is RedirectToRouteResult)) { filterContext.Controller.TempData[Key] = filterContext.Controller.ViewData.ModelState; } } base.OnActionExecuted(filterContext); } } public class ImportModelStateFromTempData : ModelStateTempDataTransfer { public override void OnActionExecuted(ActionExecutedContext filterContext) { ModelStateDictionary modelState = filterContext.Controller.TempData[Key] as ModelStateDictionary; if (modelState != null) { //Only Import if we are viewing if (filterContext.Result is ViewResult) { filterContext.Controller.ViewData.ModelState.Merge(modelState); } else { //Otherwise remove it. filterContext.Controller.TempData.Remove(Key); } } base.OnActionExecuted(filterContext); } } 
+7
source

What do you mean by “mandatory” redirects? Often we use try / catch in the controller, if the attempt is successful, you can either redirect to View (if you NEED), or also return a partial view or whatever you need. The trick often redraws the original page with an error message, as the mail request was unsuccessful.

I hope I didn’t get you wrong :)

0
source

Source: https://habr.com/ru/post/900351/


All Articles