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

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
MVC3快速搭建Web應(yīng)用(二)

上一篇文章發(fā)布后MVC3快速搭建Web應(yīng)用(一),自己又仔細(xì)讀了數(shù)遍,感覺一是文筆太差,二是描述邏輯比較混亂,客觀原因是涉及到東西其實(shí)蠻多的,那三個(gè)步驟不可能在一篇短短的文章中就可以描述清楚。此篇筆者將盡量更加詳盡一些。另外需要說明一點(diǎn)的是,本文默認(rèn)讀者:

熟悉ASP.NET MVC;Razor語法;熟悉javascript;實(shí)體框架。

Web應(yīng)用不像winform應(yīng)用,要想讓用戶得到更流暢更舒適的體驗(yàn),方法之一就是模擬winform的窗口操作,使用戶在瀏覽器中也能像桌面一樣舒服。在界面框架方面我們有大家最熟悉的jquery ui,有Ext等等,經(jīng)過一系列的篩選,我們最終決定使用easyui,文檔教程例子都比較全面的一個(gè)js ui框架。首先我們來看看用到的js文件

 
 
 
 
  1.  jquery主文件
  2.  easy ui主文件
  3.  校驗(yàn)組件 
  4.  表單組件
  5.  easyui的中文化
  6.  校驗(yàn)組件的中文化

我們把它添加到mvc的Shared/_Layout.cshtml中。這樣我們的項(xiàng)目所有Layout=null的視圖都擁有了easyui支持。

在MVC3中,當(dāng)你右鍵添加一個(gè)控制器時(shí),向?qū)?huì)讓你選擇:

其中模版我們選擇使用實(shí)體框架并生成相關(guān)actions與views,Model選擇你實(shí)體框架中對(duì)應(yīng)的表名(類名),DataContext選擇上下文類

Views引擎選擇Razor,高級(jí)選項(xiàng)里的兩個(gè)勾都去掉,因?yàn)槲覀儾恍枰脙?nèi)置的腳本庫,也不需要選擇layout(不選擇layout,MVC默認(rèn)該view使用Shared/_Layout.cshtml,也就是剛才我們添加js文件link的那個(gè)文件)。

確認(rèn)上一篇中你下載的t4模版放進(jìn)了它應(yīng)該存在的地方(最好備份一下原始的),當(dāng)你點(diǎn)擊Add時(shí),vs會(huì)自動(dòng)在Controllers下面添加相應(yīng)的控制器,在views文件夾下添加Create、Edit、Delete、Details、Index五個(gè)文件。下面我們一一查看他們的內(nèi)容:

#p#

控制器中,action已經(jīng)自動(dòng)幫你添加完畢

 
 
 
 
  1.     private BsmisEntities db = new BsmisEntities();
  2.         //
  3.         // GET: /User/
  4.         public ViewResult Index()
  5.         {
  6.             return View();
  7.         }
  8.         //
  9.         // GET: /User/Create
  10.         public ActionResult Create()
  11.         {
  12.             return View();
  13.         } 
  14.         //
  15.         // POST: /User/Create
  16.         [HttpPost]
  17.         public ActionResult Create(T_User t_user)
  18.         {
  19.             JsonResult result = new JsonResult();
  20.             result.Data = true;
  21.             try
  22.             {
  23.                 if (t_user.Enable == null)
  24.                     t_user.Enable = 0;
  25.                 db.T_User.AddObject(t_user);
  26.                 db.SaveChanges();
  27.             }
  28.             catch (Exception ee)
  29.             {
  30.                 result.Data = ee.Message;
  31.             }
  32.             return result;
  33.         }
  34.         //
  35.         // GET: /User/Edit/5
  36.         [OutputCache(Location = OutputCacheLocation.None)]
  37.         public ActionResult Edit(int id)
  38.         {
  39.             T_User t_user = db.T_User.Single(t => t.UserID == id);
  40.             ViewBag.DepartmentID = new SelectList(db.T_DepartmentInfo, "DepartmentID", "Code", t_user.DepartmentID);
  41.             return View(t_user);
  42.         }
  43.         //
  44.         // POST: /User/Edit/5
  45.         [HttpPost]
  46.         [OutputCache(Location = OutputCacheLocation.None)]
  47.         public ActionResult Edit(T_User t_user)
  48.         {
  49.             JsonResult result = new JsonResult();
  50.             result.Data = true;
  51.             try
  52.             {
  53.                 db.T_User.Attach(t_user);
  54.                 db.ObjectStateManager.ChangeObjectState(t_user, EntityState.Modified);
  55.                 db.SaveChanges();
  56.             }
  57.             catch (Exception ee)
  58.             {
  59.                 result.Data = ee.Message;
  60.             }
  61.             return result;
  62.         }
  63.         //
  64.         // POST: /User/Delete/5
  65.         [HttpPost, ActionName("Delete")]
  66.         public ActionResult DeleteConfirmed(int id)
  67.         { 
  68.        JsonResult json=new JsonResult();
  69.         json.Data=true;
  70.         try
  71.         {              T_User t_user = db.T_User.Single(t => t.UserID ==id);
  72.               db.T_User.DeleteObject(t_user);
  73.               db.SaveChanges();
  74.         }
  75.         catch(Exception ee)
  76.         {
  77.           json.Data=ee.Message;
  78.         }
  79.         return json;        }
  80.         /// 
  81.         /// 數(shù)據(jù)顯示、分頁信息
  82.         /// 
  83.         /// 
  84.         /// 
  85.         /// 
  86.         public JsonResult List(int page, int rows)
  87.         {
  88.             var q = from u in db.T_User
  89.                     join d in db.T_DepartmentInfo on u.DepartmentID equals d.DepartmentID
  90.                     orderby u.UserID
  91.                     select new
  92.                         {
  93.                             UserID = u.UserID,
  94.                             UserName = u.UserName,
  95.                             Address = u.Address,
  96.                             Birth = u.Birth,
  97.                             DepartmentID = u.DepartmentID,
  98.                             DepartmentName = d.Name,
  99.                             Enable = u.Enable,
  100.                             Gendar = u.Gendar,
  101.                             IDCardNumber = u.IDCardNumber,
  102.                             LastAccessIP = u.LastAccessIP,
  103.                             LastAccessTime = u.LastAccessTime,
  104.                             LogonTimes = u.LogonTimes,
  105.                             Password = u.Password,
  106.                             PostCode = u.PostCode,
  107.                             RealName = u.RealName,
  108.                             Tel = u.Tel,
  109.                             Province = u.Province,
  110.                             City = u.City,
  111.                             Area = u.Area
  112.                         };
  113.             var result = q.Skip((page - 1) * rows).Take(rows).ToList();
  114.             Dictionary json = new Dictionary();
  115.             json.Add("total", q.ToList().Count);
  116.             json.Add("rows", result);
  117.             return Json(json, JsonRequestBehavior.AllowGet);
  118.         }

這些action分別對(duì)應(yīng)create、delete、edit、index視圖(detail我們一般情況下不需要它,所以我的模版里沒有寫對(duì)應(yīng)的生成代碼)你可以比較一下它與原生的模版生成的代碼之間的區(qū)別。后期我們還會(huì)在控制器里添加一些譬如檢查名稱是否重名之類的action

 
 
 
 
  1. [OutputCache(Location = OutputCacheLocation.None)]
  2.         public JsonResult CheckRealNameExist(string RealName, int UserID)
  3.         {
  4.             JsonResult result = new JsonResult();
  5.             result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
  6.             result.Data = false;
  7.             try
  8.             {
  9.                 if (UserID == 0)
  10.                 {
  11.                     if (db.T_User.Any(p => p.RealName == RealName))
  12.                     {
  13.                         return result;
  14.                     }
  15.                 }
  16.                 else
  17.                 {
  18.                     if (db.T_User.Any(p => ((p.UserID != UserID) && (p.RealName == RealName))))
  19.                     {
  20.                         return result;
  21.                     }
  22.                 }
  23.             }
  24.             catch (Exception)
  25.             {
  26.                 return result;
  27.             }
  28.             result.Data = true;
  29.             return result;
  30.         }

返回值一般都是jsonresult。這樣的話,當(dāng)你在瀏覽器中訪問http://localhost:1233/User/CheckRealNameExist?RealName=張三&UserID=0時(shí) 你會(huì)獲得一個(gè)true或false值。是不是跟webservice有點(diǎn)異曲同工?

同樣,在Views文件夾中生成了Create、Edit、Details、Delete、Index五個(gè)文件,其中Details與Delete我們不需要,因?yàn)槲覀兿胧褂酶押玫漠惒絼h除(用戶單擊delete后,頁面不刷新,成功后瀏覽器下方滑出提示,3秒后關(guān)閉,失敗滑出失敗信息,不自動(dòng)關(guān)閉 /利用easyui中的messager組件)。以下是Index中的js:

#p#

 
 
 
 
  1. //刪除
  2.         function del() {
  3.             var id = getselectedRow();
  4.             if (id != undefined) {
  5.                 $.messager.confirm('確認(rèn)', '確定刪除?', function (r) {
  6.                     if (r) {
  7.                         var url = 'User/Delete/' + id;
  8.                         $.post(url, function () {
  9.                         }).success(function () {
  10.                             $.messager.show({
  11.                                 title: '提示',
  12.                                 msg: '刪除成功',
  13.                                 timeout: 3000,
  14.                                 showType: 'slide'
  15.                             });
  16.                             $('#dg').datagrid('reload');
  17.                         })
  18.                             .error(function () {
  19.                                 $.messager.alert('錯(cuò)誤', '刪除發(fā)生錯(cuò)誤');
  20.                             });
  21.                     }
  22.                 });
  23.             }
  24.         }

我們把Details與Delete刪除后只剩下Index、Create、Edit三個(gè)文件,這三個(gè)文件之間的關(guān)系是,Index中包含添加、編輯按鈕,點(diǎn)擊后使用js將對(duì)應(yīng)的actionresult加載到div中,以實(shí)現(xiàn)彈窗新建,編輯的效果。

 
 
 
 
  1. //新建
  2.         function c_dlg() {
  3.             var url = 'User/Create';
  4.             $('#c_dlg').show();
  5.             $('#c_dlg').load(url, function () {
  6.                 $(this).dialog({
  7.                     title: '添加',
  8.                     buttons: [{
  9.                         text: '提交',
  10.                         iconCls: 'icon-ok',
  11.                         handler: function () {
  12.                             $('#c_form').submit();
  13.                         }
  14.                     }, {
  15.                         text: '取消',
  16.                         handler: function () {
  17.                             $('#c_dlg').dialog('close');
  18.                         }
  19.                     }]
  20.                 });
  21.             });
  22.         }
  23.         //編輯框
  24.         function e_dlg() {
  25.             var id = getselectedRow();
  26.             if (id != undefined) {
  27.                 var url = 'User/Edit/' + id;
  28.                 $('#e_dlg').show();
  29.                 $('#e_dlg').load(url, function () {
  30.                     $(this).dialog({
  31.                         title: '編輯',
  32.                         buttons: [{
  33.                             text: '提交',
  34.                             iconCls: 'icon-ok',
  35.                             handler: function () {
  36.                                 $('#e_form').submit();
  37.                             }
  38.                         }, {
  39.                             text: '取消',
  40.                             handler: function () {
  41.                                 $('#e_dlg').dialog('close');
  42.                             }
  43.                         }]
  44.                     });
  45.                 });
  46.             }
  47.         }

這里面的c_dlg與e_dlg是index頁面的兩個(gè)Div節(jié)點(diǎn):

 
 
 
 
  • 以上的代碼完成將控制器中的action返回的頁面內(nèi)容動(dòng)態(tài)加載到div中,并以彈窗的特效顯示在當(dāng)前(Index)頁面中。效果如圖:

    我們來看看Create\Edit視圖的內(nèi)容,首先是js

    #p#

     
     
     
     

    這部分js將本頁面的控件初始化為對(duì)應(yīng)的下拉框或日期選取框等等,Html為

     
     
     
     
    1. @using (Html.BeginForm("Create", "User", FormMethod.Post, new { id = "c_form" }))
    2. {
    3.  
    4.         
    5.             
    6.                 
    7.                     @Html.LabelFor(model => model.UserName, "用戶名:")
    8.                 
    9.                 
    10.                     
    11.                    
    12.                         *
    13.                 
    14.             
    15.             
    16.                 
    17.                     @Html.LabelFor(model => model.DepartmentID, "組織機(jī)構(gòu):")
    18.                 
    19.                 
    20.                     
    21.                         *
    22.                 
    23.             
    24.             
    25.                 
    26.                     @Html.LabelFor(model => model.Password, "密碼:")
    27.                 
    28.                 
    29.                     @Html.PasswordFor(model => model.Password, new { @class = "{required:true,minlength:5}" })
    30.                         *
    31.                 
    32.             
    33.             
    34.                 
    35.                      文章標(biāo)題:MVC3快速搭建Web應(yīng)用(二)
      文章路徑:http://m.5511xx.com/article/dpogeio.html