Created
September 18, 2014 12:56
-
-
Save ianfnelson/d0e0260464145fde2dbc to your computer and use it in GitHub Desktop.
Snippets for 2010 blog post "An MVC Gotcha and the PRG Pattern"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace MvcGotcha1.Models | |
{ | |
public class FooModel | |
{ | |
public int FooCounter { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcGotcha1.Models.FooModel>" %> | |
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> | |
Foo | |
</asp:Content> | |
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> | |
<h2>Foo</h2> | |
<% using (Html.BeginForm()) | |
{ %> | |
<%=Html.TextBoxFor(m=>m.FooCounter) %><br /> | |
<%=Html.DisplayFor(m=>m.FooCounter) %><br /> | |
<input type="submit" value="Increment!" /> | |
<% } %> | |
</asp:Content> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web.Mvc; | |
using MvcGotcha1.Models; | |
namespace MvcGotcha1.Controllers | |
{ | |
public class FooController : Controller | |
{ | |
public ActionResult Index() | |
{ | |
var model = new FooModel() { FooCounter = 1 }; | |
return View(model); | |
} | |
[HttpPost] | |
public ActionResult Index(FooModel model) | |
{ | |
model.FooCounter++; | |
return View(model); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web.Mvc; | |
using MvcGotcha1.Models; | |
namespace MvcGotcha1.Controllers | |
{ | |
public class FooController : Controller | |
{ | |
public ActionResult New() | |
{ | |
var model = new FooModel() { FooCounter = 1 }; | |
Session["model"] = model; | |
return RedirectToAction("Index"); | |
} | |
public ActionResult Index() | |
{ | |
var model = Session["model"]; | |
return View(model); | |
} | |
[HttpPost] | |
public ActionResult Index(FooModel model) | |
{ | |
model.FooCounter++; | |
Session["model"] = model; | |
return RedirectToAction("Index"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment