日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
【博文推薦】ASP.NETMVC4+BootStrap實(shí)戰(zhàn)

本博文出自博客BruceAndLee博主,有任何問題請(qǐng)進(jìn)入博主頁(yè)面互動(dòng)討論!

博文地址:http://leelei.blog./856755/1587301

OK,不多說了,看一下Solution的截圖

基本上一看就明白了,控制器調(diào)用Biz層,Biz層調(diào)用DAL層,DAL層進(jìn)行數(shù)據(jù)的CURD。Utility是一些公用的類庫(kù)。ok,為什么程序集的命名都是以Bruce開頭呢,因?yàn)槲以诠镜挠⑽拿羞@個(gè)。廢話不多說,我們先看一下頁(yè)面

 

我們引入了BootStrap,主要是為了頁(yè)面布局。在Views中Partial下面放的都是部分頁(yè)。

我們先看一下運(yùn)行效果,今天主要是講頁(yè)面初始化部分。

其實(shí)查詢條件就是婚否,出生日期,姓名的模糊查詢。我們先看一下頁(yè)面Index.cshtml的代碼

 
 
  1.  
  2.  
  3.  
  4.      
  5.      
  6.      
  7.     Compare data between Solr and DB 
  8.      
  9.      
  10.           @*@Styles.Render("~/css")*@ 
  11.           @Scripts.Render("~/bundles/BootStrap") 
  12.           @Scripts.Render("~/bundles/Scripts") 
  13.      
  14.         .pre-Scrollable { 
  15.             max-height: 700px; 
  16.             overflow-y: scroll; 
  17.         } 
  18.      
  19.  
  20.  
  21.      
  22.          
  23.              
  24.                  
  25.                     Compare Data Between Solr and DB 
  26.                 
 
  •             
  •  
  •              
  •                  
  •                      
  •                      
  •                 
  •  
  •             
  •  
  •              
  •                  
  •                      
  •                      
  •                 
  •  
  •             
  •  
  •              
  •                  
  •                      
  •                      
  •                 
  •  
  •             
  •  
  •              
  •                  
  •                      
  •                         IsMarried: 
  •                         @Html.DropDownList("ddlMarried", ViewBag.MarriedList as SelectList, null, new { id = "ddlMarried", @class = "form-control" }) 
  •                     
  •  
  •                      
  •                         BirthDay: 
  •                          
  •                         
  •                          
  •                     
  •  
  •                      
  •                         Name: 
  •                          
  •                     
  •  
  •                      
  •                          
  •                     
  •  
  •                  
  •              
  •              
  •                  
  •                     @{Html.RenderPartial("~/Views/Partial/UserInfoPartial.cshtml");} 
  •                  
  •                  
  •                     @{Html.RenderPartial("~/Views/Partial/DiffAndSameWithSolrPartial.cshtml");} 
  •                  
  •              
  •          
  •      
  •  
  •  
  • 我們使用html5+BootStrap布局,這里用到了BootStrap的網(wǎng)格系統(tǒng),將瀏覽器平分為12份,即12列,很容易構(gòu)造出響應(yīng)式布局系統(tǒng)。那么什么是BootStrap的網(wǎng)格系統(tǒng),看如下的解釋

     OK,我們?cè)趺纯词欠袷琼憫?yīng)式的布局呢,我們打開谷歌瀏覽器,現(xiàn)將瀏覽器縮小到一定程度。

    看到了吧,即使設(shè)備瀏覽器這么小,我們還是能用。那我們?cè)谑謾C(jī)模擬器中測(cè)試一下,打開谷歌瀏覽器,按F12,點(diǎn)擊手機(jī)模擬器樣的東西,然后Device選擇iphone6。 

    我們看到iphone6下面的效果是這樣的。說到這里我最近很討厭兩個(gè)廣告,一個(gè)是“這個(gè)是iphone6,這個(gè)是iphone6 plus,它們都有一個(gè)叫健康的東西.....但是好吃啊”,還有一個(gè)是“當(dāng)牛魔王變成一個(gè)餃子,我愿意變成一雙筷子”??吹竭@兩個(gè)廣告,我想砸電視。

    那為什么不同的設(shè)備不同的瀏覽器都是可以正常瀏覽的呢,原因就在于這段代碼

     
     
    1.  

    這段代碼的意思是網(wǎng)頁(yè)寬度默認(rèn)等于屏幕寬度,縮放比例默認(rèn)為1(網(wǎng)頁(yè)初始比例占屏幕的100%)。

    ok,我們接下來看head部分css和js的引用,這里有個(gè)新東西叫Bundle,用來打包壓縮js或者css的。通過它打包壓縮的js或者css客戶端只需要下載一次包即可,而且可以在客戶端緩存起來,當(dāng)檢測(cè)到有更新時(shí),才會(huì)重新下載。

    下面是Bundle.cs的代碼

     
     
    1. using System.Web; 
    2. using System.Web.Optimization; 
    3.   
    4. namespace Brue.GRLC.Web 
    5.     public class BundleConfig 
    6.     { 
    7.         // 有關(guān) Bundling 的詳細(xì)信息,請(qǐng)?jiān)L問 http://go.microsoft.com/fwlink/?LinkId=254725 
    8.         public static void RegisterBundles(BundleCollection bundles) 
    9.         { 
    10.             bundles.Add(new ScriptBundle("~/bundles/BootStrap").Include( 
    11.                         "~/Scripts/jquery-1.11.1.js","~/BootStrap/js/bootstrap.js")); 
    12.   
    13.             bundles.Add(new ScriptBundle("~/bundles/Scripts").Include("~/Js/Index.js")); 
    14.   
    15.             bundles.Add(new StyleBundle("~/css").Include("~/BootStrap/css/bootstrap-theme.css" 
    16.                 , "~/BootStrap/css/bootstrap.css")); 
    17.         } 
    18.     } 

    注意,在這里引用js的時(shí)候不要引用壓縮過的js,比如xxx.min.js。當(dāng)Bundle在遇到這種js命名文件的時(shí)候,直接就忽略掉了。那么我們?cè)贖ead中只需要使用如下代碼來引用即可。

     
     
    1. @Scripts.Render("~/bundles/BootStrap") 
    2. @Scripts.Render("~/bundles/Scripts") 

    OK,在這我碰到一個(gè)問題,就是我的css通過這種方式引用,始終提示Index out of range。如果哪位大牛知道原因的話麻煩留個(gè)言,謝謝!

     

    OK,我們接下來看一下控制器代碼,頁(yè)面剛進(jìn)來,會(huì)走Home/Index。

     
     
    1. public ActionResult Index() 
    2.         { 
    3.             List marriedList = GRLCBiz.GetInstance().GetMarriedList(); 
    4.             SelectList selectList = new SelectList(marriedList, "MarriedID", "DisplayContent", "-1"); 
    5.             ViewBag.MarriedList = selectList; 
    6.   
    7.             DataResponse dataResponse = GRLCBiz.GetInstance().GetUserInfoEntityList(); 
    8.             UserInfoViewModel userInfoViewModel = new UserInfoViewModel(); 
    9.             userInfoViewModel.DataResponse = dataResponse; 
    10.             userInfoViewModel.DataResponse.PageIndex = ConstValues.CONN_DefaultPageIndex; 
    11.             userInfoViewModel.DataResponse.PageSize = ConstValues.CONN_DefaultPageSize; 
    12.             userInfoViewModel.DataResponse.StartPageIndex = 1; 
    13.             return View(userInfoViewModel); 
    14.         } 
    15. 首先我們構(gòu)造了一個(gè)SelectList用于下拉列表,Biz層的代碼很簡(jiǎn)單

       
       
      1. public dynamic GetMarriedList() 
      2.         { 
      3.             IList marriedList = new List(); 
      4.             marriedList.Add(new { MarriedID = -1, DisplayContent = "No Selection" }); 
      5.             marriedList.Add(new { MarriedID = 0, DisplayContent = "Married" }); 
      6.             marriedList.Add(new { MarriedID = 1, DisplayContent = "UnMarried" }); 
      7.   
      8.             return marriedList; 
      9.         } 
      10. 用匿名類去構(gòu)造一個(gè)List。接下來就是DataReponse的獲取,Biz層的代碼如下

         
         
        1. public DataResponse GetUserInfoEntityList(UserInfoRequest request = null) 
        2.         { 
        3.             if(request==null) 
        4.             { 
        5.                 request = new UserInfoRequest(); 
        6.                 request.PageIndex = ConstValues.CONN_DefaultPageIndex; 
        7.                 request.PageSize = ConstValues.CONN_DefaultPageSize; 
        8.             } 
        9.             
        10.             int totalCount=0; 
        11.   
        12.             List userDBEntityList = GRLCDAL.GetInstance().GetUserInfoEntityList(request, out totalCount); 
        13.             DataResponse dataResponse = new DataResponse(); 
        14.             dataResponse.DataList = userDBEntityList; 
        15.             dataResponse.TotalCount = totalCount; 
        16.             return dataResponse; 
        17.         } 

        沒什么可說的,ConstValues類中是一些靜態(tài)只讀屬性

         
         
        1. public class ConstValues 
        2.     { 
        3.         public static readonly string CON_DBConnection = ConfigurationManager.ConnectionStrings["DB_ConnectionStr"].ToString(); 
        4.         public static readonly string CON_DbScriptXmlFolder = ConfigurationManager.AppSettings["DbScriptXmlFolder"]; 
        5.         public static readonly int CONN_DefaultPageSize = int.Parse(ConfigurationManager.AppSettings["DefaultPageSize"]); 
        6.         public static readonly int CONN_DefaultPageIndex = 1; 
        7.         public static readonly int CONN_PagerDisplayCount = int.Parse(ConfigurationManager.AppSettings["PagerDisplayCount"]); 
        8.     } 

        看一下DAL層。

         
         
        1. public List GetUserInfoEntityList(UserInfoRequest request, out int totalCount) 
        2.         { 
        3.             totalCount = 0; 
        4.             string sqlScript = string.Empty; 
        5.             try 
        6.             { 
        7.                 sqlScript = DBScriptManager.GetScript(this.GetType(), "GetUserInfo"); 
        8.                 SqlParameter[] sqlParameters =  
        9.                 { 
        10.                     new SqlParameter("@IsMarried",SqlDbType.Char,1), 
        11.                     new SqlParameter("@StartDate",SqlDbType.DateTime), 
        12.                     new SqlParameter("@EndDate",SqlDbType.DateTime), 
        13.                     new SqlParameter("@UserName",SqlDbType.NVarChar,20), 
        14.                     new SqlParameter("@PageIndex",SqlDbType.Int), 
        15.                     new SqlParameter("@PageSize",SqlDbType.Int), 
        16.                     new SqlParameter("@TotalCount",SqlDbType.Int) 
        17.                 }; 
        18.   
        19.                 sqlParameters[0].Value = request.IsMarried; 
        20.                 sqlParameters[1].Value = request.StartDate; 
        21.                 sqlParameters[2].Value = request.EndDate; 
        22.                 sqlParameters[3].Value = request.UserName; 
        23.                 sqlParameters[4].Value = request.PageIndex; 
        24.                 sqlParameters[5].Value = request.PageSize; 
        25.                 sqlParameters[6].Direction = ParameterDirection.Output; 
        26.   
        27.                 DataSet ds = SqlHelper.ExecuteDataset(ConstValues.CON_DBConnection, CommandType.Text, sqlScript, sqlParameters); 
        28.                 if (ds != null && ds.Tables.Count > 0) 
        29.                 { 
        30.                     totalCount = Convert.ToInt32(sqlParameters[6].Value); 
        31.                     return ds.Tables[0].ToEntityList(); 
        32.                 } 
        33.   
        34.                 return new List(); 
        35.             } 
        36.             catch (Exception ex) 
        37.             { 
        38.                 LogHelper.WriteExceptionLog(MethodBase.GetCurrentMethod(), ex); 
        39.                 return null; 
        40.             } 
        41.         } 

        OK,我們看一下這個(gè)GetUserInfo腳本,在Bruce.GRLC.DbScriptXml程序集下。

         
         
        1.  
        2.  
        3.  

        腳本很簡(jiǎn)單,就是傳入?yún)?shù)查分頁(yè)數(shù)據(jù)。

        在DAL層我們將DataTable通過ToEntityList轉(zhuǎn)化為了實(shí)體List,在Utility中我們定義了一個(gè)擴(kuò)展用來轉(zhuǎn)化。

         
         
        1. public static class DataTableToEntityExtension 
        2.     { 
        3.         public static List ToEntityList(this DataTable dt) where T : class,new() 
        4.         { 
        5.             List entityList = new List(); 
        6.   
        7.             Type entityType = typeof(T); 
        8.             PropertyInfo[] propertys = entityType.GetProperties(); 
        9.             DataMappingAttribute mappingAttribute = null; 
        10.   
        11.             foreach (DataRow dr in dt.Rows) 
        12.             { 
        13.                 T tEntity = new T(); 
        14.   
        15.                 foreach (PropertyInfo pi in propertys) 
        16.                 { 
        17.                     mappingAttribute = pi.GetCustomAttribute(typeof(DataMappingAttribute)) as DataMappingAttribute; 
        18.   
        19.                     if (mappingAttribute != null && dt.Columns.Contains(mappingAttribute.mappingName)) 
        20.                     {  
        21.                         if (!pi.CanWrite) continue; 
        22.   
        23.                         object value = dr[mappingAttribute.mappingName]; 
        24.                         if (value != DBNull.Value) 
        25.                             pi.SetValue(tEntity, value, null); 
        26.                     } 
        27.                 } 
        28.                 entityList.Add(tEntity); 
        29.             } 
        30.             return entityList; 
        31.         } 
        32.     } 

        值那么轉(zhuǎn)化的時(shí)候是怎么讓DataTable的列和實(shí)體匹配起來,你可以將列別名和實(shí)體定義成一樣的,還有一種你可以使用Attribute。那我們使用后者,因?yàn)楹笳吒`活。

         
         
        1. [AttributeUsage(AttributeTargets.Property)] 
        2.     public class DataMappingAttribute : Attribute 
        3.     { 
        4.         public string mappingName; 
        5.         public DbType dbType; 
        6.         public DataMappingAttribute() 
        7.         { } 
        8.   
        9.         public DataMappingAttribute(string mappingName, DbType dbType) 
        10.         { 
        11.             this.mappingName = mappingName; 
        12.             this.dbType = dbType; 
        13.         } 
        14.     } 

        定義好Attribute之后,我們?cè)O(shè)置其能使用的目標(biāo)只能是Property。然后我們?cè)趯?shí)體類里面的屬性上加上這個(gè)Attribute。

         
         
        1. namespace Bruce.GRLC.Model.Entity 
        2.     public class UserDBEntity 
        3.     { 
        4.         [DataMapping("UseNo", DbType.AnsiString)] 
        5.         public string UserID { get; set; } 
        6.   
        7.         [DataMapping("Name", DbType.AnsiString)] 
        8.         public string UserName { get; set; } 
        9.   
        10.         [DataMapping("Age", DbType.Int32)] 
        11.         public int Age { get; set; } 
        12.   
        13.         [DataMapping("Married", DbType.String)] 
        14.         public string Married { get; set; } 
        15.     } 

        在DataTableToEntityExtension這個(gè)擴(kuò)展中我們得到屬性的Attribute去和DataTable的列名去匹配,反射賦值。

        OK,拿到數(shù)據(jù)后,我們?cè)诳刂破鳂?gòu)造viewModel,傳遞給界面來綁定。我們看一下部分頁(yè)UserInfoPartial.cshtml的代碼

         
         
        1. @using Bruce.GRLC.Model.ViewModel; 
        2. @model UserInfoViewModel 
        3.  
        4.      
        5.          
        6.             帳號(hào) 
        7.             姓名 
        8.             年齡 
        9.             婚否 
        10.          
        11.      
        12.      
        13.         @if (Model != null && Model.DataResponse != null && Model.DataResponse.DataList != null) 
        14.         { 
        15.             foreach (var userEntity in Model.DataResponse.DataList) 
        16.             { 
        17.                  
        18.                      
        19.                         @userEntity.UserID 
        20.                      
        21.                      
        22.                         @userEntity.UserName 
        23.                      
        24.                      
        25.                         @userEntity.Age 
        26.                      
        27.                      
        28.                         @userEntity.Married 
        29.                      
        30.                  
        31.             } 
        32.         } 
        33.      
        34.  
        35.  
        36.     @{Html.RenderPartial("~/Views/Partial/PaginationPartial.cshtml", Model.DataResponse);} 
        37.  

        其實(shí)也就是一個(gè)應(yīng)用了BoootStrap樣式的表格,有邊框和鼠標(biāo)經(jīng)過的樣式。關(guān)于BootStrap的樣式的使用,請(qǐng)參考BootStrap官網(wǎng)。代碼很簡(jiǎn)單,就是循環(huán)遍歷,展示數(shù)據(jù)。

         


        網(wǎng)站標(biāo)題:【博文推薦】ASP.NETMVC4+BootStrap實(shí)戰(zhàn)
        網(wǎng)站URL:http://www.5511xx.com/article/djshghg.html