using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Web; namespace JsonDataInMVC.DBOperator { public static class DataTableToList<T> where T : new() { public static IList<T> ConvertToModel(DataTable dt) { //定义集合 IList<T> ts = new List<T>(); T t = new T(); string tempName = ""; //获取此模型的公共属性 PropertyInfo[] propertys = t.GetType().GetProperties(); foreach (DataRow row in dt.Rows) { t = new T(); foreach (PropertyInfo pi in propertys) { tempName = pi.Name; //检查DataTable是否包含此列 if (dt.Columns.Contains(tempName)) { //判断此属性是否有set if (!pi.CanWrite) continue; object value = row[tempName]; if (value != DBNull.Value) pi.SetValue(t, value, null); } } ts.Add(t); } return ts; } } }
创建Province控制器:
using JsonDataInMVC.DBOperator; using JsonDataInMVC.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace JsonDataInMVC.Controllers { public class ProvinceController : Controller { private AddressHelper db; public ProvinceController() { db = new AddressHelper(); } // GET: Province public ActionResult Index() { List<Province> lstProvince= db.GetAllProvince(); ViewBag.ListProvince = lstProvince; return View(); } } }
对应的Index视图:
@using JsonDataInMVC.Models @{ ViewBag.Title = "Index"; List<Province> lstProvince = ViewBag.ListProvince as List<Province>; } <h2>ProvinceIndex</h2> <label>省份:</label> <select> @foreach (var item in lstProvince) { <option value="@item.ProvinceID">@item.ProvinceName</option> } </select>
修改一下,默认的路由,
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Province", action = "Index", id = UrlParameter.Optional } ); }
先来看下阶段性的成果:运行程序:
看,这样就加载了所有的省份数据,现在我们要进一步实现,选择一个省份的时候,加载数据到另外一个下拉框中。
修改控制器,添加一个方法:
using JsonDataInMVC.DBOperator; using JsonDataInMVC.Models; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace JsonDataInMVC.Controllers { public class ProvinceController : Controller { private AddressHelper db; public ProvinceController() { db = new AddressHelper(); } // GET: Province public ActionResult Index() { List<Province> lstProvince= db.GetAllProvince(); ViewBag.ListProvince = lstProvince; return View(); } public JsonResult GetAllCityByProvinceID(int id) { List<City> lstCity= db.GetCityListByProvinceID(id); return Json(lstCity, JsonRequestBehavior.AllowGet); } } }
Index视图中:
@using JsonDataInMVC.Models @{ ViewBag.Title = "Index"; List<Province> lstProvince = ViewBag.ListProvince as List<Province>; } <h2>ProvinceIndex</h2> <label>省份:</label> <select> @foreach (var item in lstProvince) { <option value="@item.ProvinceID">@item.ProvinceName</option> } </select> <br/> <hr /> <label>城市:</label> <select> </select> <script src="https://www.jb51.net/~/Scripts/jquery-1.10.2.js"></script> <script type="text/javascript"> $(document).ready(function () { $("#myProvince").change(function () { //获取省份的ID var provinceID = $("#myProvince").val(); //获取城市 var myCity=$("#myCity"); //加入测试代码 debugger; $.ajax({ url: "/Province/GetAllCityByProvinceID/" + provinceID, type: "post", dataType: "json", contentType: "application/json", success: function (result) { var myHTML = ""; myCity.html("");//赋值之前先清空 $.each(result, function (i, data) { myHTML += "<option value=" + data.CityID + ">" + data.CityName + "</option>"; }); myCity.append(myHTML); }, error: function (result) { alert(result.responseText); } }); }) }) </script>
好了,弄好之后,运行程序:
选择一个省份,对应的市的信息就被我们查出来了,绑定到另外的市的下拉框中了。