1.2 Post-Redirect-Get防止刷新页面造成的重复提交数据
在ASP.NET中要防止用户刷新页面,重复提交数据的话。需要在页面里面写JavaScript,而且要在后台c#代码中判断数据是否已经提交,才可以做到万无一失。
在ASP.NET 的 MVC框架中要实现防止刷新页面非常的简单,就是利用上面介绍的TempData来实现的。TempData用来传递数据,支持跨action传递,但是只能第一次进入action的时候访问,后面再次访问的话,TempData中的数据就会丢失。就是利用了这一点,在提交页面将提交的内容放入TempData中,然后再成功的提示页面获取出来,如果TempData["Data"]=null的话,就说明是用户在刷新页面,可以跳转到其他view或者做个提示。
具体代码如下:
实体
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Mvc3EmptyApp.Models.Entities
{
public class GuestBook
{
public string Name { get; set; }
public string Email { get; set; }
public string Comments { get; set; }
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Mvc3EmptyApp.Models.Entities
{
public class GuestBook
{
public string Name { get; set; }
public string Email { get; set; }
public string Comments { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Mvc3EmptyApp.Controllers
{
public class GuestBookController : Controller
{
//
// GET: /GuestBook/
public ActionResult Index()
{
var entity = new Models.Entities.GuestBook();
return View(entity );
}
[HttpPost]
public ActionResult Index(Models.Entities.GuestBook guest)
{
TempData["entity"] = guest;
return RedirectToAction("ThankYou");
}
public ActionResult ThankYou()
{
if (TempData["entity"] == null)
{
return RedirectToAction("Index");
}
var model = TempData["entity"] as Models.Entities.GuestBook;
return View(model);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Mvc3EmptyApp.Controllers
{
public class GuestBookController : Controller
{
//
// GET: /GuestBook/
public ActionResult Index()
{
var entity = new Models.Entities.GuestBook();
return View(entity );
}
[HttpPost]
public ActionResult Index(Models.Entities.GuestBook guest)
{
TempData["entity"] = guest;
return RedirectToAction("ThankYou");
}
public ActionResult ThankYou()
{
if (TempData["entity"] == null)
{
return RedirectToAction("Index");
}
var model = TempData["entity"] as Models.Entities.GuestBook;
return View(model);
}
}
}
新建view的时候选择强类型的view(create a strongly-typed view)
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Mvc3EmptyApp.Models.Entities.GuestBook>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<% using (Html.BeginForm())
{ %>
<p>
<%=Html.LabelFor (model=>model.Name) %>
<%=Html.TextBoxFor (model=>model.Name) %>
</p>
<p>
<%=Html.LabelFor (model=>model.Email ) %>
<%=Html.TextBoxFor (model=>model.Email ) %>
</p>
<p>
<%=Html.LabelFor (model=>model.Comments ) %>
<%=Html.TextAreaFor (model=>model.Comments ) %>
</p>
<p>
<input type="submit" value="Sign" />
</p>
<%} %>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Header" runat="server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="SideBar" runat="server">
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="Footer" runat="server">
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<% using (Html.BeginForm())
{ %>
<p>
<%=Html.LabelFor (model=>model.Name) %>
<%=Html.TextBoxFor (model=>model.Name) %>
</p>
<p>
<%=Html.LabelFor (model=>model.Email ) %>
<%=Html.TextBoxFor (model=>model.Email ) %>
</p>
<p>
<%=Html.LabelFor (model=>model.Comments ) %>
<%=Html.TextAreaFor (model=>model.Comments ) %>
</p>
<p>
<input type="submit" value="Sign" />
</p>
<%} %>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Header" runat="server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="SideBar" runat="server">
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="Footer" runat="server">
</asp:Content>
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Mvc3EmptyApp.Models.Entities.GuestBook>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
ThankYou
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>ThankYou</h2>
<%=Html.DisplayForModel() %>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Header" runat="server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="SideBar" runat="server">
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="Footer" runat="server">
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
ThankYou
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>ThankYou</h2>
<%=Html.DisplayForModel() %>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Header" runat="server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="SideBar" runat="server">
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="Footer" runat="server">
</asp:Content>