Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I preserve the url (with the querystring) after an Http Post but also add an error to the Model State?

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?

like image 763
The Vanilla Thrilla Avatar asked May 31 '13 15:05

The Vanilla Thrilla


People also ask

Can we pass URL parameters in POST request?

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.

Are QueryString parameters secure in https?

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.

How do I pass a URL with multiple parameters into a URL?

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.

What is QueryString in URL?

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.


1 Answers

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);
    }
like image 117
SharpC Avatar answered Oct 11 '22 15:10

SharpC