How to save a URL (using a request) after an HTTP message, but also add an error to the model state?

Essentially, I'm trying to authenticate a user by providing their account and their social security number. If they enter the wrong combination, I do the following in the post Authenticate post:

 ModelState.AddModelError("Authenticated", authenticationError); return View(); 

An error is displayed here, but then I lose what was in my query string. An alternative to storing a query string is:

 ModelState.AddModelError("Authenticated", authenticationError); return Redirect(Request.Url + "?returnUrl=" + returnUrl); 

This will contain a query string, but the error will not be displayed. I assume this happened because ModelState has changed.

I need returnUrl because the user is forced to the Authenticate page whenever he clicks a button to view a specific event. I want to configure it so that they still go to this event as soon as they authenticate.

Is there a way I can achieve both saving the query string and displaying a model error?

+4
source share
1 answer

The second scenario has no model state, because when you redirect, the browser makes a separate request to this location, separate requests = the new state of the model.

I would suggest using your first script and placing "ReturnUrl" in your model and displaying it to the client as a hidden field.

 //In your model add the ReturnUrl Property public class AuthenticatModel { public string Account {get; set;} public string SocialSecurityNumber {get;set;} public string ReturnUrl {get;set;} } ModelState.AddModelError("Authenticated", authenticationError); //Set the return URL property before returning the view model.ReturnUrl = returnUrl; return View(model); @* add the return URL as a hidden field to your view so it can be posted back *@ @Html.HiddenFor(model => model.ReturnUrl) 
+4
source

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


All Articles