eWorld.UI - Matt Hawley

Ramblings of Matt

MVC Post-Redirect-Get Sample Updated

June 5, 2008 08:56 by matthaw

I took some time to look back at my MVC Post-Redirect-Get sample and see where it could be improved as well as update it to use the MVC Preview 3 bits. What I found, is again, the core concepts didn't change that much. However, there are some new enhancements that Preview 3 gave us that makes our life a little bit easier. I'll save the full implementation for you to download and checkout yourself, but I do want to highlight some of these enhancements that made the source easier to use.

1. NameValueCollectionExtensions.CopyTo - this made it very nice for me to take all of the posted form data and copy it into the TempData so that upon a redirect, I could extract it out and put it in ViewData.

   1:  if (!BaseValidator.Validate(HttpContext.Request, validators)) {
   2:     NameValueCollectionExtensions.CopyTo(Request.Form, TempData);
   3:     TempData["ErrorMessage"] = BuildErrorMessage(validators);
   4:     return RedirectToAction("Create");
   5:  }

2. I added an extension method for IDictionary to copy between a source and destination, primarily for copying my TempData to ViewData. This way there is no need to do a manual copy of TempData objects all over the place, and is more resilient to changes and additions within your views and controller actions. I'm hoping this makes it into the MVC stack at some point so we all don't have to write this code ourselves.

   1:  public static void CopyTo(this IDictionary<string, object> source, 
   2:                            IDictionary<string, object> destination)
   3:  {
   4:      foreach (KeyValuePair<string, object> pair in source)
   5:      {
   6:          if (!destination.ContainsKey(pair.Key))
   7:              destination.Add(pair.Key, pair.Value);
   8:      }
   9:  }

The above code allows us to simply call the following line of code

   1:  TempData.CopyTo(ViewData);

3. What you'll notice, is that now all of my data is within ViewData, I can start to utilize the built-in functionality added in Preview 3 where the form controls will attempt to extract an initial value from ViewData. This mechanism really brings back the concept of ViewState, except that there is really no overhead to do this! Here's how my form now looks

   1:  <%@ Import Namespace="PRG.Controllers" %>
   2:  <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
   3:  <% using (Html.Form<ProductsController>(c => c.Submit())) { %>
   4:      <h3>Create a New Product</h3>
   5:      Name: <%= Html.TextBox("Name") %><br />
   6:      Price: <%= Html.TextBox("Price") %><br />
   7:      Quantity: <%= Html.TextBox("Quantity") %><br />
   8:      <% if (ViewData["ErrorMessage"] != null) { %>
   9:          <br /><span style="color:red"><%= ViewData["ErrorMessage"] %></span><br />
  10:      <% } %>
  11:      <br />
  12:      <%= Html.SubmitButton() %>
  13:  <% } %>
  14:  </asp:Content>

4. While this next item isn't specifically related to Preview 3, it is a change from my last sample. Previously, I was doing all of my manual validations inline on the server, and it wasn't pretty. As you've probably been reading, I've been making some improvements to the MVC Validation within MvcContrib, and I decided I'd bring in that codebase to this sample. However, to truly show the full PRG pattern, I needed my form to post and alert me that there are errors on the page rather than relying upon client side validation; so I'm simply using the validator objects & server validation. In the next coming weeks, I'll be making another update to MvcContrib to do model based validation. I'll leave this code for your viewing or other examples on my blog.

And that's the updated sample. You can downloaded the latest bits from here. Please let me know what you think and anything else you would do to change this. As each iteration of the MVC framework is released, the sample gets easier and easier! Hope you enjoy this.



kick it on DotNetKicks.com

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

June 6. 2008 04:30

Pingback from weblogs.asp.net

MVC Post-Redirect-Get Sample Updated - eWorld.UI - Matt Hawley

weblogs.asp.net

March 11. 2009 01:21

so great!

wensten

March 20. 2009 08:08

Pingback from lemtoks.dreamhosters.com

ASP.NET MVC — RC1 — Паттерн Post-Redirect-Get « Test

lemtoks.dreamhosters.com

May 29. 2009 10:53

nices article.

saç ekimi merkezleri

May 31. 2009 14:32

Great article thanks for sharing

yazılım

June 9. 2009 00:19

so great!

wenste

Fotoğraf Makinası

June 10. 2009 18:03

Great article thanks for sharing.

body building blog

June 16. 2009 10:16

<a href="http://www.ftkcambalkon.com"" rel="nofollow">http://www.ftkcambalkon.com" title="cam balkon">cam balkon</a>
<a href="http://www.ftkcambalkon.com"" rel="nofollow">http://www.ftkcambalkon.com" title="cambalkon">cambalkon</a>

cam balkon

June 16. 2009 10:16

<a href="http://www.pendikbilisim.com"" rel="nofollow">http://www.pendikbilisim.com" title="pendik">pendik </a>
<a href="http://www.pendikbilisim.com"" rel="nofollow">http://www.pendikbilisim.com" title="pendik bilgisayar">pendik bilgisayar</a>

pendik

June 16. 2009 10:17

1 <a href="http://www.perdelicambalkon.co.cc" title="cam balkon">cam balkon</a>
2 <a href="http://www.katlanircam.co.cc" title="cam balkon">cam balkon</a>
3 <a href="http://www.balkoncam.co.cc" title="cam balkon">cam balkon</a>
4 <a href="http://www.balkoncamlama.co.cc" title="cam balkon">cam balkon</a>
5 <a href="http://www.cambalkonsistemleri.co.cc" title="cam balkon">cam balkon</a>
6 <a href="http://www.balkoncami.co.cc" title="cam balkon">cam balkon</a>
7 <a href="http://www.cambalkonrehber.co.cc" title="cam balkon">cam balkon</a>
8 <a href="http://www.cambalkonfirmalari.co.cc" title="cam balkon">cam balkon</a>
9 <a href="http://www.cambalkonkapama.co.cc" title="cam balkon">cam balkon</a>
10 <a href="http://www.cambalkonkapatma.co.cc" title="cam balkon">cam balkon</a>
11 <a href="http://www.terascamlama.co.cc" title="cam balkon">cam balkon</a>
12 <a href="http://www..cafecamlama.co.cc" title="cam balkon">cam balkon</a>
13 <a href="http://www.cambalkonankara.co.cc" title="cam balkon">cam balkon</a>
14 <a href="http://www.cambalkonistanbul.co.cc" title="cam balkon">cam balkon</a>
15 <a href="http://www.cambalkonbursa.co.cc" title="cam balkon">cam balkon</a>
16 <a href="http://www.cambalkonizmir.co.cc" title="cam balkon">cam balkon</a>
17 <a href="http://www.camlamabalkon.co.cc"" rel="nofollow">http://www.camlamabalkon.co.cc" title="cam balkon">cam balkon</a>
18 <a href="http://www.katlanabilirbalkon.co.cc" title="cam balkon">cam balkon</a>
19 <a href="http://www.katlanabilircam.co.cc" title="cam balkon">cam balkon</a>
20 <a href="http://www.cambalkonum.co.cc" title="cam balkon">cam balkon</a>
21 <a href="http://www.cambalkoncum.co.cc" title="cam balkon">cam balkon</a>
22 <a href="http://www.cam-balkon.co.cc" title="cam balkon">cam balkon</a>
23 <a href="http://www.camlamabalkon.co.cc"" rel="nofollow">http://www.camlamabalkon.co.cc" title="cam balkon">cam balkon</a>
24 <a href="http://www.babilcambalkon.com" title="cam balkon">cam balkon</a>25
<a href="http://www.cambalkon.us" title="cam balkon">cam balkon</a>
25 <a href="http://www.cambalkon.in" title="cam balkon">cam balkon</a>
26 <a href="http://www.dunyacambalkon.com" title="cam balkon">cam balkon</a>

cam balkon

June 26. 2009 09:51





<a href="http://www.cinema.gen.tr"" rel="nofollow">http://www.cinema.gen.tr" title="cinema">cinema</a>
<a href="http://www.cinema.gen.tr"" rel="nofollow">http://www.cinema.gen.tr" title="transformers">transformers</a>

transformers

June 26. 2009 09:51

<a href="http://www.movies.gen.tr"" rel="nofollow">http://www.movies.gen.tr" title="movies">movies</a>
<a href="http://www.movies.gen.tr"" rel="nofollow">http://www.movies.gen.tr" title="transformers">transformers</a>

transformers

July 1. 2009 01:39

good thanks.

cam balkon

Add comment


 

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

July 3. 2009 21:21



Copyright © 2000 - 2009 , Excentrics World