您的位置:首页 > 编程语言 > ASP

ASP.NET MVC1.0设计连载

2010-10-14 23:27 162 查看
从下图可以看出是首先通过view的请求,经过url导航到一个controller中,最终定位到一个action,在action中访问具体的model,获取数据之后,将数据放入ViewData或者是TempData中,然后通过action呈现到指定的view上。在view上可以定制显示的格式。

1 知识点
1.1 ViewData和TempData的区别
ViewData只能在本Action内有效,在本Action中可以保存数据。
TempData可以在Action跳转中使用,TempData的数据在服务器的Session中保存,但是只保留一个来回。也就是第一次跳转过去的时候还可以访问,后面就没有了。范围限制在同一个Controller中的不同Action传递数据。
public class HomeController : Controller {public ActionResult Index2() {[b][b] ViewData[[/b][/b]"ViewData"= "我是VeiwData中的数据"; TempData["TempData"] = "我是TempData中的数据"; return View("Index2"); }public ActionResult Index3(string name) {[b] [b] Models.ViewModels.UserViewModel userVM[/b][/b]=new Models.ViewModels.UserViewModel() {[b] Name[/b]=name };[b] return View(userVM );[b] }[b] }[/b][/b][/b]

在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中要防止用户刷新页面,重复提交数据的话。需要在页面里面写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; } }[b]}[/b]

using System;using System.Collections.Generic;[b]using System.Linq;[b]using System.Web;[b]using System.Web.Mvc;[b][b]namespace Mvc3EmptyApp.Controllers[b]{[/b][/b][/b][/b][/b][/b]public class GuestBookController : Controller {//// GET: /GuestBook/public ActionResult Index() {[b] var entity [/b]= new Models.Entities.GuestBook(); return View(entity );[b] }[b] [HttpPost][/b][/b]public ActionResult Index(Models.Entities.GuestBook guest) {[b] TempData[[/b]"entity"] = guest; return RedirectToAction("ThankYou"); }public ActionResult ThankYou() {if (TempData["entity"] == null) {[b] return RedirectToAction([/b]"Index"); }[b] var model [/b]= TempData["entity"] as Models.Entities.GuestBook; return View(model);[b] }[b] }[b]}[/b][/b][/b]

新建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>[/b]本文出自 “Apprentice” 博客,请务必保留此出处http://apprentice.blog.51cto.com/2214645/1360629
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: