快过年了,给大家拜个早年.做了个jQgrid的DEMO。哈哈。
主要运用MVC 和 jQgrid(后期会加上Nhibernate 以及spring.Net)
环境 W-XP SP3+ VS2008SP1 + Oracle.
准备工作:
下载:
1、jquery.jqGrid-3.8.1.zip;
2、jquery-ui-1.8.6.custom2.zip;(这个样式可随便下载)
(以上2个包以及源码在附件当中)
打开ORACLE 创建数据库
SqlCode
--Create the province table and add some data
create table province
(
ProvinceID int,
ProvinceCode varchar(20),
ProvinceName varchar(20),
primary key (ProvinceID)
);
select * from province; insert into province
select 1001 , 1001,'北京' from dual
union all
select 1002 , 1002,'天津' from dual
union all
select 1003 , 1003,'重庆' from dual
union all
select 1004 , 1004,'四川' from dual
union all
select 1005 , 1005,'辽宁' from dual;
commit; ----Create the city table and add some data
Create table city
(
CityID int,
CityCode varchar(20),
CityName varchar(20),
ProcinceID int,
primary key (CityID)
); insert into city
select 101 , 101,'北京市',1001 from dual
union all
select 102 , 102,'朝阳',1001 from dual
union all
select 103 , 103,'海淀',1001 from dual
union all
select 104 , 104,'昌平',1001 from dual
union all
select 105 , 105,'天津市',1002 from dual
union all
select 106 , 106,'重庆市',1003 from dual
union all
select 107 , 107,'南坪',1003 from dual
union all
select 108 , 108,'李家沱',1003 from dual
union all
select 109 , 109,'鱼洞',1003 from dual
union all
select 110 , 110,'界石',1003 from dual
union all
select 111 , 111,'成都市',1004 from dual
union all
select 112 , 112,'天府软件园',1004 from dual
union all
select 113, 113,'华阳镇',1004 from dual
union all
select 114 , 114,'沈阳市',1005 from dual
union all
select 115 , 115,'大连市',1005 from dual;
commit;
create table province
(
ProvinceID int,
ProvinceCode varchar(20),
ProvinceName varchar(20),
primary key (ProvinceID)
);
select * from province; insert into province
select 1001 , 1001,'北京' from dual
union all
select 1002 , 1002,'天津' from dual
union all
select 1003 , 1003,'重庆' from dual
union all
select 1004 , 1004,'四川' from dual
union all
select 1005 , 1005,'辽宁' from dual;
commit; ----Create the city table and add some data
Create table city
(
CityID int,
CityCode varchar(20),
CityName varchar(20),
ProcinceID int,
primary key (CityID)
); insert into city
select 101 , 101,'北京市',1001 from dual
union all
select 102 , 102,'朝阳',1001 from dual
union all
select 103 , 103,'海淀',1001 from dual
union all
select 104 , 104,'昌平',1001 from dual
union all
select 105 , 105,'天津市',1002 from dual
union all
select 106 , 106,'重庆市',1003 from dual
union all
select 107 , 107,'南坪',1003 from dual
union all
select 108 , 108,'李家沱',1003 from dual
union all
select 109 , 109,'鱼洞',1003 from dual
union all
select 110 , 110,'界石',1003 from dual
union all
select 111 , 111,'成都市',1004 from dual
union all
select 112 , 112,'天府软件园',1004 from dual
union all
select 113, 113,'华阳镇',1004 from dual
union all
select 114 , 114,'沈阳市',1005 from dual
union all
select 115 , 115,'大连市',1005 from dual;
commit;
数据库创建好了后 看下系统的结构图(除Nhibernate、Spring.net):
Common 文件里存放的是准备实现分页的部分.
Content中的Themes 包含jQgrid的主题.
Extension中包含的是MVC有无分页的JSON数据组装.
其他应该就是基本的MVC结构了。
总体效果预览:
Edit
1、先把两个实体建立好
建立Entities文件夹 在其下建立两个实体类 分别为City,Province.
City.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security; namespace Marlboro.Web.Entities
{
public class City
{
public int City_ID { get; set; }
public string City_Code { get; set; }
public string City_Name { get; set; }
public int province_ID { get; set; }
public bool IsActive { get; set; }
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security; namespace Marlboro.Web.Entities
{
public class City
{
public int City_ID { get; set; }
public string City_Code { get; set; }
public string City_Name { get; set; }
public int province_ID { get; set; }
public bool IsActive { get; set; }
}
}
Province.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security; namespace Marlboro.Web.Entities
{
public class Province
{
public int Province_ID { get; set; }
public string Province_Code { get; set; }
public string Province_Name { get; set; }
public bool Province_IsActive { get; set; }
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security; namespace Marlboro.Web.Entities
{
public class Province
{
public int Province_ID { get; set; }
public string Province_Code { get; set; }
public string Province_Name { get; set; }
public bool Province_IsActive { get; set; }
}
}
2、在Controller下建立CityController
CityController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; using Marlboro.Web.Entities;
using Marlboro.Web.Extension; namespace Marlboro.Web.Controllers
{
public class CityController : Controller
{
//
// GET: /City/
public ActionResult Index()
{
return View();
}
/// 返回绑定的DROPDOWNLIST数据
/// </summary>
/// <returns></returns>
public ActionResult Citylist()
{
ViewData["ProvinceList"] = new Marlboro.Web.Models.CityModels().GetDropdownlistItem();
return View();
}
/// <summary>
/// 通过省的ID 取出所有城市的JSON格式
/// </summary>
/// <param name="provinceId"></param>
/// <returns></returns>
public JsonResult Getlist(int? provinceId)
{
List<City> Citylist = new Marlboro.Web.Models.CityModels().Citylist(provinceId);
var retVal = new Extension.BuildJson<City, int>(
Citylist,
city => city.City_ID,
city => "",
city => city.City_ID.ToString(),
city => city.City_Code,
city => city.City_Name,
city => city.province_ID.ToString()
).Build();
return Json(retVal, JsonRequestBehavior.AllowGet);
}
{
{
//[0000055] bug fix
string CityID = collection["CityID"];
string code = collection["CityCode"];
string name = collection["CityName"];
string Provinceid = collection["ProvinceID"];
{
return this.Content("City code is required!");
}
if (string.IsNullOrEmpty(name))
{
return this.Content("City name is required!");
}
City c = new Marlboro.Web.Models.CityModels().GetCitylistByID(Convert.ToInt32(CityID));
if (c == null)
{
return this.Content("The city to be update has been deleted by another user.");
}
c.City_ID = Convert.ToInt32(CityID);
c.City_Code = code;
c.City_Name = name;
c.province_ID = Convert.ToInt32(Provinceid);
}
catch (Exception ex)
{
//this.Response.Headers["exceptionMessage"] = ex.Message;
//throw ex;
return this.Content(ex.Message);
}
return this.Content("");
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; using Marlboro.Web.Entities;
using Marlboro.Web.Extension; namespace Marlboro.Web.Controllers
{
public class CityController : Controller
{
//
// GET: /City/
public ActionResult Index()
{
return View();
}
/// <summary>
/// 返回绑定的DROPDOWNLIST数据
/// </summary>
/// <returns></returns>
public ActionResult Citylist()
{
ViewData["ProvinceList"] = new Marlboro.Web.Models.CityModels().GetDropdownlistItem();
return View();
}
/// <summary>
/// 通过省的ID 取出所有城市的JSON格式
/// </summary>
/// <param name="provinceId"></param>
/// <returns></returns>
public JsonResult Getlist(int? provinceId)
{
List<City> Citylist = new Marlboro.Web.Models.CityModels().Citylist(provinceId);
var retVal = new Extension.BuildJson<City, int>(
Citylist,
city => city.City_ID,
city => "",
city => city.City_ID.ToString(),
city => city.City_Code,
city => city.City_Name,
city => city.province_ID.ToString()
).Build();
return Json(retVal, JsonRequestBehavior.AllowGet);
}
[HttpPost]
{
try
{
//[0000055] bug fix
string CityID = collection["CityID"];
string code = collection["CityCode"];
string name = collection["CityName"];
string Provinceid = collection["ProvinceID"];
if (string.IsNullOrEmpty(code))
{
return this.Content("City code is required!");
}
if (string.IsNullOrEmpty(name))
{
return this.Content("City name is required!");
}
City c = new Marlboro.Web.Models.CityModels().GetCitylistByID(Convert.ToInt32(CityID));
if (c == null)
{
return this.Content("The city to be update has been deleted by another user.");
}
c.City_ID = Convert.ToInt32(CityID);
c.City_Code = code;
c.City_Name = name;
c.province_ID = Convert.ToInt32(Provinceid);
new Marlboro.Web.Models.CityModels().UpdateCityByID(c);
}
catch (Exception ex)
{
//this.Response.Headers["exceptionMessage"] = ex.Message;
//throw ex;
return this.Content(ex.Message);
}
return this.Content("");
}
}
}
3、Models (暂时先写的SQL语句写的比较垃圾。后期打算合并NhiberNate)
CityModels.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Data;
using System.Collections; using Marlboro.Web.Entities; namespace Marlboro.Web.Models
{
public class CityModels
{
/// 获取所有省信息 绑定Dropdownlist
/// </summary>
/// <returns></returns>
public List<SelectListItem> GetDropdownlistItem()
{
List<SelectListItem> list = new List<SelectListItem>();
DataTable Itemsdt = new DataTable();
Itemsdt = new DataBaseTest.OracleHelper().ExecuteDataTable("select * from PROVINCE");
if (Itemsdt.Rows.Count != 0)
{
for (int i = 0; i < Itemsdt.Rows.Count; i++)
{
list.Add(new SelectListItem { Text = Itemsdt.Rows[i]["ProvinceName"].ToString(), Value = Itemsdt.Rows[i]["ProvinceID"].ToString() });
}
}
return list ?? null;
}
/// 通过procinveId 获取City列表
/// </summary>
/// <param name="provinceId"></param>
/// <returns></returns>
public List<City> Citylist(int? provinceId)
{
DataTable dt = new DataTable();
dt = new DataBaseTest.OracleHelper().ExecuteDataTable("select * from City where ProcinceID='" + provinceId + "'");//where ProcinceID='" + provinceID + "'
if (dt.Rows.Count != 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
City city = new City();
city.City_ID = Convert.ToInt32(dt.Rows[i]["CITYID"]);
city.City_Code = dt.Rows[i]["CITYCODE"].ToString();
city.City_Name = dt.Rows[i]["CITYNAME"].ToString();
city.province_ID = Convert.ToInt32(dt.Rows[i]["PROCINCEID"]);
Citylist.Add(city);
}
}
return Citylist;
}
/// 通过CityID 获取实体
/// </summary>
/// <param name="CityID"></param>
/// <returns></returns>
public City GetCitylistByID(int? CityID)
{
City city = new City();
DataTable dt = new DataTable();
dt = new DataBaseTest.OracleHelper().ExecuteDataTable("select * from City where CITYID='" + CityID + "'");//where ProcinceID='" + provinceID + "'
if (dt.Rows.Count != 0)
{
city.City_ID = Convert.ToInt32(dt.Rows[0]["CITYID"]);
city.City_Code = dt.Rows[0]["CITYCODE"].ToString();
city.City_Name = dt.Rows[0]["CITYNAME"].ToString();
city.province_ID = Convert.ToInt32(dt.Rows[0]["PROCINCEID"]);
}
return city;
}
/// 更新City数据 提供给前台JS
/// </summary>
/// <param name="city"></param>
public void UpdateCityByID(City city)
{
string sql = "update City set CITYCODE='" + city.City_Code + "',CITYNAME='" + city.City_Name + "',PROCINCEID='" + city.province_ID + "' where CITYID='" + city.City_ID + "'";
new DataBaseTest.OracleHelper().ExcuteCity(sql);
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Data;
using System.Collections; using Marlboro.Web.Entities; namespace Marlboro.Web.Models
{
public class CityModels
{
/// <summary>
/// 获取所有省信息 绑定Dropdownlist
/// </summary>
/// <returns></returns>
public List<SelectListItem> GetDropdownlistItem()
{
List<SelectListItem> list = new List<SelectListItem>();
DataTable Itemsdt = new DataTable();
Itemsdt = new DataBaseTest.OracleHelper().ExecuteDataTable("select * from PROVINCE");
if (Itemsdt.Rows.Count != 0)
{
for (int i = 0; i < Itemsdt.Rows.Count; i++)
{
list.Add(new SelectListItem { Text = Itemsdt.Rows[i]["ProvinceName"].ToString(), Value = Itemsdt.Rows[i]["ProvinceID"].ToString() });
}
}
return list ?? null;
}
/// <summary>
/// 通过procinveId 获取City列表
/// </summary>
/// <param name="provinceId"></param>
/// <returns></returns>
public List<City> Citylist(int? provinceId)
{
List
<City> Citylist = new List<City>();DataTable dt = new DataTable();
dt = new DataBaseTest.OracleHelper().ExecuteDataTable("select * from City where ProcinceID='" + provinceId + "'");//where ProcinceID='" + provinceID + "'
if (dt.Rows.Count != 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
City city = new City();
city.City_ID = Convert.ToInt32(dt.Rows[i]["CITYID"]);
city.City_Code = dt.Rows[i]["CITYCODE"].ToString();
city.City_Name = dt.Rows[i]["CITYNAME"].ToString();
city.province_ID = Convert.ToInt32(dt.Rows[i]["PROCINCEID"]);
Citylist.Add(city);
}
}
return Citylist;
}
/// <summary>
/// 通过CityID 获取实体
/// </summary>
/// <param name="CityID"></param>
/// <returns></returns>
public City GetCitylistByID(int? CityID)
{
City city = new City();
DataTable dt = new DataTable();
dt = new DataBaseTest.OracleHelper().ExecuteDataTable("select * from City where CITYID='" + CityID + "'");//where ProcinceID='" + provinceID + "'
if (dt.Rows.Count != 0)
{
city.City_ID = Convert.ToInt32(dt.Rows[0]["CITYID"]);
city.City_Code = dt.Rows[0]["CITYCODE"].ToString();
city.City_Name = dt.Rows[0]["CITYNAME"].ToString();
city.province_ID = Convert.ToInt32(dt.Rows[0]["PROCINCEID"]);
}
return city;
}
/// <summary>
/// 更新City数据 提供给前台JS
/// </summary>
/// <param name="city"></param>
public void UpdateCityByID(City city)
{
string sql = "update City set CITYCODE='" + city.City_Code + "',CITYNAME='" + city.City_Name + "',PROCINCEID='" + city.province_ID + "' where CITYID='" + city.City_ID + "'";
new DataBaseTest.OracleHelper().ExcuteCity(sql);
}
}
}
4、建立文件夹Extension ,在文件夹下建立BuildJson类
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq; using System.Collections;
using System.Collections.Generic;
using Marlboro.Web.Common; namespace Marlboro.Web.Extension
{
public class BuildJson<TEntity, IdType>
{
/// <summary>
/// MVC 无分页 Json数据构造
/// </summary>
/// <param name="entities">对象实体集合</param>
/// <param name="idFunc">泛型委托,传入匿名函数lambda 传入TEntity 返回 IdType</param>
/// <param name="propertyFuncs">泛型委托集合 用法同上</param>
public BuildJson(
List<TEntity> entities,
Func<TEntity, IdType> idFunc,
params Func<
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq; using System.Collections;
using System.Collections.Generic;
using Marlboro.Web.Common; namespace Marlboro.Web.Extension
{
public class BuildJson<TEntity, IdType>
{
/// <summary>
/// MVC 无分页 Json数据构造
/// </summary>
/// <param name="entities">对象实体集合</param>
/// <param name="idFunc">泛型委托,传入匿名函数lambda 传入TEntity 返回 IdType</param>
/// <param name="propertyFuncs">泛型委托集合 用法同上</param>
public BuildJson(
List<TEntity> entities,
Func<TEntity, IdType> idFunc,
params Func<