`

应用ASP.NET MVC的系统软件架构-知识普及篇1

阅读更多

 

应用MVC的系统软件架构

 

          -----基础知识

 

引言

 

 

M-V-C(Model View Controller)

 

 

 

/Files/virusswb/应用MVC的系统软件架构.doc

 

   从上图可以看出是首先通过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传递数据。

 

代码
<!--<br/ /> <br/ /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ /> http://www.CodeHighlighter.com/<br/ /> <br/ /> -->public class HomeController : Controller
    {
        
public ActionResult Index2()
        {

            ViewData[
"ViewData"= "我是VeiwData中的数据";
            TempData[
"TempData"= "我是TempData中的数据";
            
return View("Index2");
        }
        
public ActionResult  Index3(string name)
        {
            
            Models.ViewModels.UserViewModel userVM
=new Models.ViewModels.UserViewModel()
            {
                Name
=name
            };
            
return View(userVM );
        }
    }

 

  在Index2和Index3两个View中分别加入下面的代码,就是显示一下ViewData和TempData中的内容。

  为了显示效果,在Index2的View中加入下面的代码。<%Html.RenderAction("Index3"); %>  这句直接呈现Index3的View,直接可以看到效果。

<!--<br/ /> <br/ /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ /> http://www.CodeHighlighter.com/<br/ /> <br/ /> --> <div>
      1 <%= ViewData["ViewData"%><br />
   2 <%=TempData["TempData"%>
    
</div>
    
<br />
    
<%Html.RenderAction("Index3"); %>

 

 

  在Index3的View中加入下面的代码

<!--<br/ /> <br/ /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ /> http://www.CodeHighlighter.com/<br/ /> <br/ /> --> <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或者做个提示。

  具体代码如下:

  实体

  

 

实体
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Mvc3EmptyApp.Models.Entities
{
    
public class GuestBook
    {
        
public string Name { getset; }
        
public string Email { getset; }
        
public string Comments { getset; }
    }
}

 

 

  

 

Controller
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->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)

 

Index View
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--><%@ 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>

 

 

   

 

ThankYou View
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />--><%@ 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>

 

 

 

   代码下载:/Files/virusswb/Mvc3EmptyApp.rar

分享到:
评论

相关推荐

    asp.net mvc demo[25-day]

    asp.net mvc demo 2015-08-25

    基于ASP.NET MVC的三层架构博客网站系统源码

    资源名:基于ASP.NET MVC的三层架构博客网站系统源码 资源类型:程序源代码 源码说明: 使用ASP.NET写的三层架构博客系统 带SEO优化 完整代码 适合学习使用 适合人群:新手及有一定经验的开发人员

    ASP.NET MVC 5入门指南(中文PDF+源码)

    ASP.NET MVC 5入门指南 (中文PDF+源碼) 1. ASP.NET MVC 5 - 开始MVC 5之旅 2. ASP.NET MVC 5 - 控制器 3. ASP.NET MVC 5 - 视图 4. ASP.NET MVC 5 - 将数据从控制... ASP.NET MVC 5 - 使用Wijmo MVC 5模板1分钟创建应用

    asp.net MVC5 (bootstrap-table+分页+日期控件)

    asp.net MVC5 (bootstrap-table+分页+日期控件,功能都已经实现),可直接打开,需要VS2013及以上版本。

    ASP.NET MVC MSDN 文档 CHM

    ASP.NET MVC 1.0 MSDN Reference CHM Version ASP.NET MVC 1.0 MSDN 参考及类库,CHM版本,从MSDN 官方网站下载并编译。源分支URL: http://msdn.microsoft.com/en-us/library/dd394709.aspx 内容列表: ASP.NET MVC...

    精通asp.net mvc 5中文版-part1

    精通asp.net mvc 5中文版,英文名pro asp.net mvc 5 由于上传权限限制,分为两个包上传,需要全部下载后才能解压 这是第一部分,压缩工具winrar 5.4 英文版32位

    asp.net MVC三层架构

    Model(模型)是应用程序中用于处理应用程序数据逻辑的部分。 通常模型对象负责在数据库中存取数据。 View(视图)是应用程序中处理数据显示的部分。 通常视图是依据模型数据创建的。 Controller(控制器)是应用...

    Asp.Net MVC案例教程

    Asp.Net MVC案例教程 Asp.Net MVC案例教程 Asp.Net MVC案例教程 Asp.Net MVC案例教程 Asp.Net MVC案例教程 Asp.Net MVC案例教程

    ASP.NET MVC企业实战源代码Chapter12.rar

    本书共分为12章,以符合初学者思维的方式系统地介绍ASP.NET MVC的应用技巧,并结合实际项目详细地介绍如何基于ASP.NET MVC构建企业项目。通过本书的学习,读者可以全面掌握ASP.NET MVC的开发,并从代码中获取软件...

    ASP.NET MVC4 Web 编程-中文版

    ASP.NET MVC4 Web 编程-中文版,值得一看

    ASP.NET MVC 4 插件化架构简单实现

    ASP.NET MVC 4 插件化架构简单实现-思路篇 http://blog.csdn.net/hao_ds/article/details/42102969 ASP.NET MVC 4 插件化架构简单实现-实例篇 http://blog.csdn.net/hao_ds/article/details/42103111

    asp.net MVC4 CMS

    asp.net MVC4 CMS 完整的源代码,学习和提高asp.net mvc4可以参考一下。

    ASP.NET MVC5.pdf

    本系列共11篇文章,翻译自ASP.NET MVC 5 官方教程,由于本系列文章言简意赅,篇幅适中,从一个web网站示例开始讲解,全文最终完成了一个管理影片的小系统,非常适合新手入门ASP.NET MVC 5 (新增、删除、查询、更新) ...

    ASP.NET MVC项目实例

    ASP.NET MVC作为微软官方的.NET平台下MVC解决方案,自诞生起就吸引了众多.NET平台开发人员的眼球。ASP.NET MVC从一开始的设计思路就与Struts不同,它的映射是利用路由配置而非xml,从而大大降低了开发复杂度,并且比...

    dwz框架 asp.net mvc3

    dwz框架 asp.net mvc3;dwz框架 asp.net mvc3;dwz框架 asp.net mvc3

    [ASP.NET MVC] ASP.NET MVC 4 实战 (英文版)

    ASP.NET MVC 4 in Action is a hands-on guide that shows you how to apply ASP.NET MVC effectively. After a high-speed ramp up, this thoroughly revised new edition explores each key topic with a self-...

    [ASP.NET MVC] ASP.NET MVC 5 高级程序设计 (英文版)

    The ASP.NET MVC 5 Framework is the latest evolution of Microsoft’s ASP.NET web platform. It provides a high-productivity programming model that promotes cleaner code architecture, test-driven ...

    asp.net mvc后台管理系统 数据库

    asp.net mvc后台管理系统 数据库

    pro-asp.net-mvc-5-platform-master.zip

    精通ASP.NET MVC5 随书源码 新手学习使用 容易学会 结合书籍看看

    ASP.NET-MVC-3-RTM-Release-Notes.doc

    关于ASP.NET-MVC-3-RTM-Release-Notes的描述

Global site tag (gtag.js) - Google Analytics