4

Is there a way to modify the query string/URL parameters for a request in ASP.NET MVC 4 Controller before returning the view? I want to append a parameter to the URL.

I tried adding a key to the Request.QueryString dictionary, but it seems to be read-only.


Additional context information:

I have an ASP.NET MVC 4 page where the user can create an event in a calendar view. When the user clicks the "Create event" button, the system creates a pending reservation for the event. The user is then redirected to an "Edit event" view. The actual calendar event is created over the pending reservation when the user has filled the "Edit event" page and submits.

My problem is that I don't want to create a new pending reservation every time the "Edit event" page is loaded (e.g. refreshing with F5). Thus, I came up with the idea to add the newly-created pending reservation Id to the query string. This way every consecutive page-load will use the existing pending reservation.

However, it doesn't seem to be possible to edit the query string in the Controller. Is there an alternative way to do this?

public ActionResult CreateEvent()
{
    var model = new CalendarEventEditModel();

    //This should be true for the first time, but false for any consecutive requests
    if (Request.QueryString["pendingReservationId"] == null)
    {
        model.PendingReservationId = _calendarService.CreatePendingReservation();
        //The following line throws an exception because QueryString is read-only
        Request.QueryString["pendingReservationId"] = model.PendingReservationId.ToString();
    }

    return View("EditEvent", model);
}

Also any suggestions regarding the overall functionality are appreciated.

3 Answers 3

3

You should use Post/Redirect/Get pattern to avoid duplicate/multiple form submission.

Something like this:

[HttpPost]
public ActionResult CreateEvent(CreateEventViewModelSomething model)
{
    // some event reservation/persistent logic
    var newlyReservedEventId = _calendarService.CreatePendingReservation();
    return RedirectToAction("EditEvent", new { id = newlyReservedEventId });
}

public ActionResult EditEvent(int id)
{
    var model = new CalendarEventEditModel();
    model.PendingReservationId = id;
    return View(model);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I realized the root of my problem was that I was creating a resource on a GET request. Your answer helped me solve it by separating the POST and GET requests.
2

The query string is what the browser sends you. You can't modify it on the server; it's already been sent.

Instead, redirect to the same route, including the newly created query string.

Comments

1

Use this:

return this.RedirectToAction
  ("EditEvent", model, new { value1 = "queryStringValue1" });

Would return:

/controller/EditEvent?value1=queryStringValue1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.