ASP.NET MVC: Returning a view with querystring in tact

Posted by ajbeaven on Stack Overflow See other posts from Stack Overflow or by ajbeaven
Published on 2010-05-19T22:53:10Z Indexed on 2010/05/19 23:00 UTC
Read the original article Hit count: 352

I'm creating a messaging web app in ASP.NET and are having some problems when displaying an error message to the user if they go to send a message and there is something wrong.

A user can look through profiles of people and then click, 'send a message'. The following action is called (url is /message/create?to=username) and shows them a page where they can enter their message and send it:

public ActionResult Create(string to)
{
    ViewData["recipientUsername"] = to;

    return View();
}

On the page that is displayed, the username is entered in to a hidden input field. When the user clicks 'send':

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(string message, string to)
{
    try
    {
        //do message stuff that errors out
    }
    catch
    {
        ModelState.AddModelErrors(message.GetRuleViolations()); //adding errors to modelstate
    }

    return View();
}

So now the error message is displayed to the user fine, however the url is changed in that it no longer has the querystring (/message/create). Again, this would be fine except that when the user clicks the refresh button, the page errors out as the Create action no longer has the 'to' parameter.

So I'm guessing that I need to maintain my querystring somehow. Is there any way to do this or do I need to use a different method altogether?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about querystring