Skip to content

Instantly share code, notes, and snippets.

@ianfnelson
Created September 18, 2014 12:56
Show Gist options
  • Save ianfnelson/d0e0260464145fde2dbc to your computer and use it in GitHub Desktop.
Save ianfnelson/d0e0260464145fde2dbc to your computer and use it in GitHub Desktop.
Snippets for 2010 blog post "An MVC Gotcha and the PRG Pattern"
using System;
namespace MvcGotcha1.Models
{
public class FooModel
{
public int FooCounter { get; set; }
}
}
<%@ 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>
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);
}
}
}
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