tags:

views:

94

answers:

2

I want to use a simple URL query parameter to display customized confirmation messages like "Message sent" or "Message deleted". I want to model it similar to how Flickr does it. Their URLs look like http://www.flickr.com/photos/username/?success=1 and then show a success message.

How do I go about doing this in ASP.NET MVC using a Controller's return View(modelObject)? The overloaded methods for return View() aren't very helpful.

A: 

You'll need to add a name/value pair into the ViewData dictionary, e.g.

ViewData["message"] = "EPIC FAIL!!!";

Then access the same in your view:

Your result is: <%= ViewData["message"] %>
Josh Kodroff
+3  A: 

URL query parameters only make sense on links (i.e. <a> tags), so this is something you would create in the view not the controller (the control could supply the information to the view of course).

Using the HTML Helpers that take an object to specify URL content, any parameters that are not picked up by the route are added as query parameters. Given route:

/{controller}/{action}/{id}

then the helper code:

<%= Html.ActionLink("Click Here!", "theAction", "theController",
                    new { id = 10, q = "Something" }) %>

will create link:

/theController/theAction/10?q=Something
Richard
There's a small typo in the second code block: The semicolon after the "10" should be a comma: new { id = 10, q = "Something" }
Eilon
Thanks, Richard. This will be useful for other parts of my application, but I needed to pass a confirmation message to the view after the Controller was successful.
tridium
@tridium: Thanks, corrected
Richard