目录
接着之前未写完的继续,本篇,我将讲解在此UI框架中和ASP.NET MVC4进行结合开发。效果如下:
这里,我将添加和修改用了两个不同的视图,当然也可以把添加和修改放到同一个视图中,但是要写一些业务逻辑代码来区分当前调用的是修改还是添加,根据添加和修改的不同,而对界面进行不同的操作。
添加控制器Customer,关于更新操作,我就不得不想吐槽一下NHibernate,他妹的,每次都要先load一次,然后再Update()一次,如果你直接save,它就把你表中有,但是界面上没有传过来的值全部更新为null了,相比之下EF就好多了。
public class CustomerController : Controller { private string message = ""; //消息,是否关闭弹出窗,是否停留在当前分页(0,1) #region 客户管理主页 public ActionResult Index() { return View(); } ////// 客户列表 /// /// ///[HttpPost] public JsonResult List(CustomerFilter filter) { filter.PageSize = int.MaxValue; var dataSource = CustomerInfo.GetByFilter(filter); List queryData = dataSource.ToList(); var data = queryData.Select(u => new { ID = u.ID, CusCode = u.CusCode, CusName = u.CusName, BusssinessType = u.BusssinessType.GetDescription(false), Balance = u.Balance, CreditAmount = u.CreditAmount, Status = u.Status.GetDescription(false), Country = u.Country, CompanyName = u.CompanyName, Delivery = GetDeliveryList(u.ExpressCurInfoBy) }); //构造成Json的格式传递 var result = new { iTotalRecords = queryData.Count, iTotalDisplayRecords = 10, data = data }; return Json(result, JsonRequestBehavior.AllowGet); } #region 添加客户 /// /// 添加客户 /// /// ///public ActionResult AddCustomer() { ViewBag.Title = "添加客户"; return View(); } /// /// 添加客户 /// /// ///[HttpPost] public ActionResult AddCustomer(CustomerInfo info) { string msg = string.Empty; if (ModelState.IsValid) { try { info.Save(); msg = "添加客户成功。"; } catch (Exception ex) { msg = "添加客户失败!" + ex.Message; ViewBag.Msg = string.Format(message, msg, false,"0"); } ViewBag.Msg = string.Format(message, msg, true,"0"); } return View(); } #endregion #region 修改客户 /// /// 修改客户 /// /// ///public ActionResult UpdateCustomer(int id) { ViewBag.Title = "修改客户"; var result = CustomerInfo.Load(id); return View(result); } /// /// 修改客户 /// /// ///[HttpPost] public ActionResult UpdateCustomer(CustomerInfo info) { string msg = string.Empty; if (ModelState.IsValid) { try { info.Update(); msg = "修改客户成功。"; } catch (Exception ex) { msg = "修改客户失败!" + ex.Message; ViewBag.Msg = string.Format(message, msg, false,"1"); } ViewBag.Msg = string.Format(message, msg, true,"1"); } return View(); } #endregion }
添加视图Index
@{ ViewBag.Title = "客户信息";}@using (Html.BeginForm("List", null, FormMethod.Get, new { @clase = "form-inline", @role = "form" })) {客户信息
- 客户代码:@Html.TextBox("CusCode", "", new { @class = "trade-time wid153" })
- 客户名称:@Html.TextBox("CusName", "", new { @class = "trade-time" })
}
添加AddCustomer视图,之前公司ASP.NET MVC的项目没有启用模型验证,界面验证代码都是自己js写的,我晕,那用ASP.NET MVC干嘛呢?使用框架就是要充分发挥框架优良的功能,尽可能高效快速的开发,并减少开发人员的代码量。
@model Core.Customer.CustomerInfo@using ProjectBase.Utils@Html.Raw(ViewBag.Msg)@*@ViewBag.Title*@@using (Html.BeginForm("AddCustomer", "Customer", FormMethod.Post, new { @clase = "form-inline", @role = "form", name = "from1" })) {}
客户代码: @Html.TextBoxFor(x => x.CusCode, new { @class = "trade-timen", @id = "cusCode" })* @Html.ValidationMessageFor(m => m.CusCode) 客户名称: @Html.TextBoxFor(x => x.CusName, new { @class = "trade-timen", @id = "cusName" })* @Html.ValidationMessageFor(m => m.CusName) 手机: @Html.TextBoxFor(x => x.Phone, new { @class = "trade-timen" }) 电话: @Html.TextBoxFor(x => x.Tel, new { @class = "trade-timen" }) 邮箱: @Html.TextBoxFor(x => x.Email, new { @class = "trade-timen", @id = "email" })@Html.ValidationMessageFor(m => m.Email) 传真: @Html.TextBoxFor(x => x.Fax, new { @class = "trade-timen" }) 国家: @Html.TextBoxFor(x => x.Country, new { @class = "trade-timen" }) 地址: @Html.TextBoxFor(x => x.Address, new { @class = "trade-timen" }) 公司名称: @Html.TextBoxFor(x => x.CompanyName, new { @class = "trade-timen" }) 业务类型: @Html.DropDownListFor(x => x.BusssinessType, @Html.EnumToList(typeof(Core.Customer.Busssiness), false), new { @class = "trade-timen", style = "width:180px" }) 是否启用: 是 @Html.RadioButtonFor(x => x.Status, "0", new { Checked = "checked", @name = "status" }) 否 @Html.RadioButtonFor(x => x.Status, "1", new { @name = "status" })
添加UpdateCustomer视图
@model Core.Customer.CustomerInfo@using ProjectBase.Utils@Html.Raw(ViewBag.Msg)@*@ViewBag.Title*@@using (Html.BeginForm("UpdateCustomer", "Customer", FormMethod.Post, new { @clase = "form-inline", @role = "form", name = "from1" })) {}
客户代码: @Html.TextBoxFor(x => x.CusCode, new { @class = "trade-timen", @id = "cusCode", @readOnly = "readOnly" })* @Html.ValidationMessageFor(m => m.CusCode) @Html.HiddenFor(x => x.ID)客户名称: @Html.TextBoxFor(x => x.CusName, new { @class = "trade-timen", @id = "cusName" })* @Html.ValidationMessageFor(m => m.CusName) 手机: @Html.TextBoxFor(x => x.Phone, new { @class = "trade-timen" }) 电话: @Html.TextBoxFor(x => x.Tel, new { @class = "trade-timen" }) 邮箱: @Html.TextBoxFor(x => x.Email, new { @class = "trade-timen", @id = "email" }) @Html.ValidationMessageFor(m => m.Email) 传真: @Html.TextBoxFor(x => x.Fax, new { @class = "trade-timen" }) 国家: @Html.TextBoxFor(x => x.Country, new { @class = "trade-timen" }) 地址: @Html.TextBoxFor(x => x.Address, new { @class = "trade-timen" }) 公司名称: @Html.TextBoxFor(x => x.CompanyName, new { @class = "trade-timen" }) 业务类型: @Html.DropDownListFor(x => x.BusssinessType, @Html.EnumToList(typeof(Core.Customer.Busssiness), false), new { @class = "trade-timen", style = "width:180px" }) 是否启用: 是 @Html.RadioButtonFor(x => x.Status, "0", new { Checked = "checked", @name = "status" }) 否 @Html.RadioButtonFor(x => x.Status, "1", new { @name = "status" })
客户实体CustomerInfo
////// 客户信息 /// public class CustomerInfo //: DomainObject{ #region property /// /// 客户代码 /// [Required(ErrorMessage = "客户代码不能为空!")] [StringLength(30, MinimumLength = 0, ErrorMessage = "客户代码最大长度为30个字符")] public virtual string CusCode { get; set; } ////// 客户名称 /// [Required(ErrorMessage = "客户名称不能为空!")] [StringLength(30, MinimumLength = 0, ErrorMessage = "客户名称最大长度为30个字符")] public virtual string CusName { get; set; } ////// 客户业务类型 /// public virtual Busssiness BusssinessType { get; set; } ////// 手机 /// public virtual string Phone { get; set; } ////// 电话 /// public virtual string Tel { get; set; } ////// 邮箱 /// [RegularExpression(@"^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$", ErrorMessage="邮箱格式不正确!")] public virtual string Email { get; set; } ////// 传真 /// public virtual string Fax { get; set; } ////// 国家 /// public virtual string Country { get; set; } ////// 地址 /// public virtual string Address { get; set; } ////// 公司名称 /// public virtual string CompanyName { get; set; } ////// 金额 /// public virtual decimal Balance { get; set; } ////// 信用额度 /// public virtual decimal CreditAmount { get; set; } ////// 状态 /// public virtual CustomerStatus Status { get; set; } ////// 快件收货商信息 /// public virtual IListExpressCurInfoBy { get; set; } #endregion #region common method /// /// 分页获取数据 /// /// ///public static IPageOfList GetByFilter(CustomerFilter filter) { return Dao.GetByFilter(filter); } #endregion }
查询类CustomerFilter
public class CustomerFilter : ParameterFilter { ////// 客户代码 /// public virtual string CusCode { get; set; } ////// 客户名称 /// public virtual string CusName { get; set; } ////// 生产NHQL查询语句 /// ///public override string ToHql() { string hql = ""; if (!string.IsNullOrEmpty(CusCode)) { hql += " and Cus_Code =:CusCode "; } if (!string.IsNullOrEmpty(CusName)) { hql += " and Cus_Name =:CusName "; } return hql; } /// /// 构造查询参数 /// ///public override Dictionary GetParameters() { var result = new Dictionary (); if (!string.IsNullOrEmpty(CusCode)) { result["CusCode"] = CusCode.Trim(); } if (!string.IsNullOrEmpty(CusName)) { result["CusName"] = CusName.Trim(); } return result; } }using System;using System.Collections.Generic;using System.Linq;using System.Text;using ProjectBase.Utils.Entities;namespace ProjectBase.Data{ public abstract class ParameterFilter { public ParameterFilter() { HasQueryString = false; PageSize = 10; } public string OrderBy { get;set; } public abstract string ToHql(); public override string ToString() { return ToHql(); } public abstract Dictionary GetParameters(); public string GetOrderString() { if (OrderBy.HasValue()) return " Order By " + OrderBy; return String.Empty; } protected string GetLike(string value) { return "%" + value + "%"; } public int PageIndex { get; set; } public int PageSize { get; set; } /// /// 标识此构造器是包含全部查询语句。 /// 若为 False,则ToHql() 只需要构造条件查询,系统会自动在前面加上 public bool HasQueryString { get; set; } protected static bool HasValue(string str) { return str.HasValue(); } public static bool HasValue" from " + typeof(T).Name + " a where 1=1 "
/// 若为 True, ToHql() 需要返回 连form在类的完整Hql语句 ///(System.Nullable value) where T:struct { return value.HasValue; } }}
在这里,我只演示了控制器和视图的交互,至于Hhibernate和Unity等数据的操作,这里暂时不讲,因为你也可以使用其它的ORM框架和IOC框架,诸如EF、AutoFac等等。这里主要讲解jquery datatables和ASP.NET MVC的结合使用,但是这里只演示了客户端分页排序,后面我会讲服务器分页排序。我发现,网上都没有ASP.NET MVC和Datatables结合的完整的服务器分页、排序的Demo,只看到PHP的。于是我不断的尝试,皇天不负有心人,终于试验成功了,后面我会为大家讲述实现方式。