1 知识点
1.1 ViewData和TempData的区别
ViewData只能在本Act
TempData可以在Act
public
在Index2和Index3两个View中分别加入下面的代码,就是显示一下ViewData和TempData中的内容。
为了显示效果,在Index2的View中加入下面的代码。 这句直接呈现Index3的View,直接可以看到效果。
<div> 1 <%= ViewData["ViewData"] %><br /> 2 <%=TempData["TempData"] %> </div> <br /> <%Html.RenderAction("Index3"); %>
在Index3的View中加入下面的代码
<h2>ViewPage1</h2> <%=Model.Name %> <br /> <div> 1 <%= ViewData["ViewData"] %><br /> 2 <%=TempData["TempData"] %> </div>
结果就是
大家注意看上图中的两个红色框,第一个框中显示都有数据,第二个框中显示只有TempData中还有数据。
1.2 Post-Redirect-Get防止刷新页面造成的重复提交数据
在ASP.NET中要防止用户刷新页面,重复提交数据的话。需要在页面里面写JavaS
在ASP.NET 的 MVC框架中要实现防止刷新页面非常的简单,就是利用上面介绍的TempData来实现的。TempData用来传递数据,支持跨act
具体代码如下:
实体
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;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>
<%@ 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>