记录使用过的MVC
记录下工作中用MVC写的一个机器信息管理平台。
1. 业务需求
- 搭建一个组内平台,管理组员名下所有的VM,内容包括,VM名称、基本系统信息、所安装的主要测试工具版本、VM主人、VM主要用途
- 要求页面可编辑,可增删改查VM信息
2. 使用技术
- Visual Studio 2013/MVC/C#
- SQL Server
进入正题,大家都知道MVC框架技术分为Model, View, Controller三层,在View视图呈现层的基本代码呈现如下:
1 @model PagedList.IPagedList<Advent.GenevaQA.Dashboard.Data.GenevaMachineStatu> 2 @using PagedList.Mvc; 3 4 5 @using (Html.BeginForm("Index", "Machine", FormMethod.Post)) 6 { 7 @Html.AntiForgeryToken() 8 9 <div class="panel panel-primary"> 10 <div class="panel-heading"> 11 <h3 class="panel-title" style="color:white"><strong>Geneva QA Machine List</strong></h3> 12 </div> 13 <div class="panel-body"> 14 15 <table> 16 @using (Html.BeginForm()) 17 { 18 @Html.AntiForgeryToken() 19 @Html.ValidationSummary(true) 20 21 <tr> 22 <td>@Html.Label("Machine Name : ", new { style = "font-weight:strong" })</td> 23 <td>@Html.TextBox("search", null, new { @class = "", placeholder = "Partial or full machine name" })</td> 24 <td> 25 <button type="submit" value="Index" name="action" class="btn btn-default">Find</button> 26 </td> 27 <td> 28 <button type="button" onclick="location.href=‘@Url.Action("Create", "Machine")‘;" class="btn btn-default">AddNewMachine</button> 29 </td> 30 </tr> 31 } 32 </table> 33 34 @if (Model != null && Model.Count() > 0) 35 { 36 <div id="sourcelist"> 37 <table class="table table-striped table-hover"> 38 <tr> 39 <th> 40 Name 41 </th> 42 <th> 43 Owner 44 </th> 45 <th> 46 OS/bit 47 </th> 48 <th> 49 SQL 50 </th> 51 <th> 52 IE 53 </th> 54 <th> 55 Purpose 56 </th> 57 <th> 58 Management 59 </th> 60 </tr> 61 @foreach (var item in Model) 62 { 63 <tr> 64 <td> 65 @Html.ActionLink(item.Machine_Name, "Details", new { machineName = item.Machine_Name }) 66 </td> 67 <td> 68 @Html.DisplayFor(modelItem => item.Owner) 69 </td> 70 <td> 71 @Html.DisplayFor(modelItem => item.OS_Version_Bit) 72 </td> 73 <td> 74 @Html.DisplayFor(modelItem => item.SQL_Server) 75 </td> 76 <td> 77 @Html.DisplayFor(modelItem => item.IE) 78 </td> 79 <td> 80 @Html.DisplayFor(modelItem => item.Purpose) 81 </td> 82 <td> 83 @Html.ActionLink("Update", "Edit", new { machineName = item.Machine_Name }) | 84 @Html.ActionLink("Delete", "Delete", new { machineName = item.Machine_Name }) 85 </td> 86 </tr> 87 } 88 </table> 89 @Html.PagedListPager(Model, page => Url.Action("Index", "Machine", new { page })) 90 </div> 91 } 92 else 93 { 94 <table class="table table-striped table-hover"> 95 <tr> 96 <td> 97 <div class="alert alert-warning">No Records</div> 98 </td> 99 </tr> 100 </table> 101 } 102 103 </div> 104 105 </div> 106 }
- 第一行代码中 model的实例化很重要
- using块的巧妙使用
Model层:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 6 namespace Advent.GenevaQA.Dashboard.Models 7 { 8 public class CreateMachineMode 9 { 10 11 public string Machine_Name { get; set; } 12 public string Owner { get; set; } 13 public string Status { get; set; } 14 public string Domain { get; set; } 15 public string Purpose { get; set; } 16 public string OS_Version_Bit { get; set; } 17 public int? Memory_GB_ { get; set; } 18 public int? Hard_Disk_GB_ { get; set; } 19 public string Special_Software { get; set; } 20 public string VS_Version { get; set; } 21 public string SQL_Server { get; set; } 22 public string IE { get; set; } 23 public string Comments { get; set; } 24 public string Updated_by { get; set; } 25 public DateTime? CreatTime { get; set; } 26 public DateTime? UpdateTime { get; set; } 27 28 } 29 }
Controller层:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using Advent.GenevaQA.Dashboard.Data; 7 using Advent.GenevaQA.Dashboard.Data.DBHelper; 8 using PagedList; 9 using Advent.GenevaQA.Dashboard.Models; 10 using System; 11 using System.Configuration; 12 using System.Runtime.InteropServices; 13 14 namespace Advent.GenevaQA.Dashboard.Controllers 15 { 16 public class MachineController : Controller 17 { 18 int pageSize = 15; 19 // 20 // GET: /Machine/ 21 22 public ActionResult Index(string search, int? page) 23 { 24 IEnumerable<GenevaMachineStatu> list = null; 25 string filter = string.Empty; 26 27 if (page == null) 28 { 29 Session["search"] = search; 30 filter = search; 31 } 32 else 33 { 34 filter = Convert.ToString(Session["search"]); 35 } 36 37 using(GenevaMachineManagement gmm = new GenevaMachineManagement()) 38 { 39 //if page = 0 or null, pageNumber =1, else pageNumber = page 40 int pageNumber = (page ?? 1); 41 42 if (!string.IsNullOrEmpty(filter)) 43 { 44 List<GenevaMachineStatu> mList = new List<GenevaMachineStatu>(); 45 //list = gmm.GetAllMachines().Where(x => (x.Machine_Name+x.OS_Version_Bit+x.SQL_Server+x.Special_Software+x.Owner+x.IE).ToLower().Contains(search.ToLower())).ToPagedList(pageNumber, pageSize); 46 foreach (GenevaMachineStatu model in gmm.GetAllMachines()) 47 { 48 if (string.Format(model.Machine_Name + model.Owner + model.OS_Version_Bit + model.SQL_Server + model.Special_Software + model.IE).ToLower().Contains(filter.ToLower())) 49 { 50 mList.Add(model); 51 } 52 } 53 list = mList.ToPagedList(pageNumber, pageSize); 54 } 55 else 56 { 57 list = gmm.GetAllMachines().ToPagedList(pageNumber, pageSize); 58 } 59 } 60 61 return View(list); 62 } 63 } 64 }
这里的pageNumber的定义需要注意下,涉及到翻页刷新的问题。
数据库
使用ADO.NET Entity Data Model链接数据库后增加相应的增删改查方法即可:
1 using System; 2 using System.Collections.Generic; 3 using System.Data.Entity.Infrastructure; 4 using System.Data.Entity.Validation; 5 using System.Linq; 6 using System.Text; 7 8 namespace Advent.GenevaQA.Dashboard.Data.DBHelper 9 { 10 public class GenevaMachineManagement:IDisposable 11 { 12 GenQADashboardEntities entity = new GenQADashboardEntities(); 13 14 /// <summary> 15 /// 16 /// </summary> 17 /// <param name="machine"></param> 18 public void AddMachine(GenevaMachineStatu machine) 19 { 20 try 21 { 22 GenevaMachineStatu newmachine = new GenevaMachineStatu(); 23 newmachine.Machine_Name = machine.Machine_Name; 24 newmachine.Owner = machine.Owner; 25 newmachine.Comments = machine.Comments; 26 newmachine.CreatTime = machine.CreatTime; 27 newmachine.Domain = machine.Domain; 28 newmachine.Hard_Disk_GB_ = machine.Hard_Disk_GB_; 29 newmachine.IE = machine.IE; 30 newmachine.Memory_GB_ = machine.Memory_GB_; 31 newmachine.OS_Version_Bit = machine.OS_Version_Bit; 32 newmachine.Purpose = machine.Purpose; 33 newmachine.Special_Software = machine.Special_Software; 34 newmachine.SQL_Server = machine.SQL_Server; 35 newmachine.Status = machine.Status; 36 newmachine.Updated_by = machine.Updated_by; 37 newmachine.UpdateTime = machine.UpdateTime; 38 newmachine.VS_Version = machine.VS_Version; 39 40 entity.GenevaMachineStatus.Add(newmachine); 41 entity.SaveChanges(); 42 } 43 catch (DbEntityValidationException dbvex) 44 { 45 46 } 47 catch (DbUpdateException dbuex) 48 { 49 50 } 51 catch (InvalidOperationException ioex) 52 { 53 54 } 55 catch (NotSupportedException nsex) 56 { 57 58 } 59 } 60 61 /// <summary> 62 /// 63 /// </summary> 64 /// <returns></returns> 65 public List<GenevaMachineStatu> GetAllMachines() 66 { 67 List<GenevaMachineStatu> machines = null; 68 69 var query = from m in entity.GenevaMachineStatus 70 select m; 71 72 if (query != null) 73 machines = query.ToList(); 74 75 return machines; 76 } 77 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。