Essentially want I'm trying to do is authenticate a user by having them enter their account and their social security number. If they enter an incorrect combination, I do the following on the Authenticate
post action:
ModelState.AddModelError("Authenticated", authenticationError);
return View();
This displays the error, but then I lose what was in my query string. An alternative to keep the query string is:
ModelState.AddModelError("Authenticated", authenticationError);
return Redirect(Request.Url + "?returnUrl=" + returnUrl);
This will keep the query string, but the error will not display. I assume this is because the ModelState
has changed.
I need the returnUrl
because the user is forced to the Authenticate
page whenever they click to view a specific event. I want to set it up so that they still go to this event once they authenticate themselves.
Is there a way I can achieve both the preservation of the query string and display the model error?
If you know the URL parameters for your form post when the HTML page is sent to the client, you can tack those URL parameters on to the form's action attribute, otherwise JavaScript can set the URL parameters when the form is submitted.
An encrypted HTTPS request protects most things: This is the same for all HTTP methods (GET, POST, PUT, etc.). The URL path and query string parameters are encrypted, as are POST bodies.
To add a parameter to the URL, add a /#/? to the end, followed by the parameter name, an equal sign (=), and the value of the parameter. You can add multiple parameters by including an ampersand (&) between each one.
A query string is a set of characters tacked onto the end of a URL. The query string begins after the question mark (?) and can include one or more parameters. Each parameter is represented by a unique key-value pair or a set of two linked data items. An equals sign (=) separates each key and value.
Ivan Korytin's answer was the best (and only answer I could find which seemed to actually work properly without using hidden field hacks) which I've improved a little with Request.QueryString
.
You have to put the parameters as part of the form action:
<form action="@Url.Action("CreateEntity", "Employee")?@(Request.QueryString)"
enctype="multipart/form-data" method="POST">
When you perform the following the query string (and GET
parameters) are now preserved:
[HttpPost]
public ActionResult MyAction(MyAction model)
{
if (!ModelState.IsValid)
{
return View(model);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With