网站导航免费论文 原创论文 论文搜索 原创论文 网学软件 学术大家 资料中心 会员中心 问题解答 原创论文 大学论文导航 设计下载 最新论文 下载排行 原创论文 论文源代码
返回网学首页
网学联系
最新论文 推荐专题 热门论文 素材专题
当前位置: 网学 > 编程文档 > ASP.net > 正文

详解ASP.NET MVC 3新的Layout布局系统

来源:http://myeducs.cn 联系QQ:点击这里给我发消息 作者: 用户投稿 来源: 网络 发布时间: 13/01/06
~/Views/Home/Index.cshtml中更改为如下内容:

  1. @{  
  2.     ViewBag.Title = "首页";     
  3.     //  
  4.     // some code  
  5.     //  
  6. }  
  7. @section SectionA{  
  8.     <div>这里是SectionA:也不需要写神马runat="server"啊,有木有</div> 
  9. }  
  10. @section SectionB{  
  11.     <div>这里是SectionB:也不需要写神马&lt;asp:Content /&gt啊,有木有</div> 
  12. }  
  13. <div> 
  14.     这里就是渲染Body啦.~~不需要写神马&lt;asp:Content /&gt;,其实因为RenderBody()不在有歧义.  
  15. </div> 

最后显示的页面效果:

498)this.width=498;'' onmousewheel = ''javascript:return big(this)'' title="image" border="0" alt="image" src="/uploadfile/201301/6/1A184942495.png" />


498)this.width=498;'' onmousewheel = ''javascript:return big(this)'' title="image" border="0" alt="image" src="/uploadfile/201301/6/1A184942116.png" />

问:为什么为什么要推荐方式1呢?
答:因为RenderSection()方法有2个重载.

如果使用第一个只接受一个string类型参数的重载的话.~如果你在具体的View中没有利用@section来定义实现的话,运行会报错.所以需要配合另外一个方法IsSectionDefined()来使用,才能防止报错.

而使用第2个重载就可以利用第二个bool类型的参数指明该Section是否为必须的.所以可以在使用该RenderSection方法的时候直接利用第二个重载,再把bool参数值设为false,即使你在具体的View中没有声明实现@section,运行起来也一如既往地蛋定,不Show黄页.

IV:关于前篇文章中有热心的观众朋友们问到如何在Layout(MasterPage)中读取数据库并初始化页面的问题的解答

在这里只是做个一简单的示范,新建一个类文件,替换如下:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Data;  
  6. using System.Data.SqlClient; //在这里就用ADO.NET方式吧.EF我接触不久!  
  7.  
  8. namespace Mvc3Application1  
  9. {  
  10.     public class ReaderSQL_Date  
  11.     {  
  12.         private static readonly string _SQL_CONN_STR = "server=.\\mssqlserver,1433;uid=sa;pwd=yourpwd;database=student;";  
  13.  
  14.         public static IList<StudentEntity> GetAllStudent()  
  15.         {  
  16.             //这里仅仅是做演示,生产环境并不这样写  
  17.             using (SqlConnection conn = new SqlConnection(_SQL_CONN_STR))  
  18.             {  
  19.                 SqlCommand cmd = conn.CreateCommand();  
  20.                 cmd.CommandType = CommandType.Text;  
  21.                 cmd.CommandText = "SELECT [Sno],[Sname],[Sage] FROM [dbo].[STUDENT]";  
  22.  
  23.                 IList<StudentEntity> result = new List<StudentEntity>();  
  24.                 conn.Open();  
  25.                 using (SqlDataReader sdr = cmd.ExecuteReader())  
  26.                 {  
  27.                     while (sdr.Read())  
  28.                     {  
  29.                         result.Add(new StudentEntity  
  30.                         {  
  31.                             S_No = sdr.GetInt32(0),  
  32.                             S_Name = sdr.GetString(1),  
  33.                             S_Age = sdr.GetInt32(2)  
  34.                         });  
  35.                     }  
  36.                 }  
  37.                 //SqlConnection.ClearPool(conn); //可选清理连接池.  
  38.  
  39.                 return result;  
  40.             }  
  41.         }  
  42.     }  
  43.  
  44.     public class StudentEntity  
  45.     {  
  46.         public int S_No { getset; }  
  47.         public string S_Name { getset; }  
  48.         public int S_Age { getset; }  
  49.     }  

_

网学推荐

免费论文

原创论文

设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号