直接上代码: public class DP_City return source; public JsonResult GetProvinceSource()
代码如下:
public class DP_Provice
{
public int proviceID { get; set; }
public string ProviceName { get; set; }
public int ProviceNode { get; set; }
public virtual List
}
{
public int CityNode { get; set; }
public string CityName { get; set; }
public string ProviceNode { get; set; }
}
对以上涉及到的实体类予以赋值,暂时使用一个静态类初始化简单数据:
代码如下:
public static class DPDataSource
{
public static List
{
List
{
new DP_Provice{ ProviceNode=1, ProviceName="北京", citySource=new List
new DP_City{
CityNode=11, CityName="北京海淀"
},
new DP_City{
CityNode=12,CityName="北京西城"
}
}},
new DP_Provice{ ProviceNode=2, ProviceName="山东", citySource=new List
new DP_City{
CityNode=21, CityName="济南"
},
new DP_City{
CityNode=22,CityName="德州"
}
}},
new DP_Provice{ ProviceNode=3, ProviceName="河北", citySource=new List
new DP_City{
CityNode=31, CityName="石家庄"
},
new DP_City{
CityNode=32,CityName="衡水"
}
}}
};
}
}
具体在Controller中的调用,因为使用的JQuery中的AJAX方式,所以返回的结果类型为Json;
代码如下:
public ActionResult Index()
{
return View("DPShow");
}
List
{
if (source == null || source.Count < 0)
{
source = DPDataSource.InitalData();
}
return Json(source, JsonRequestBehavior.AllowGet);
}
public JsonResult GetCitySource(string proviceName)
{
source = DPDataSource.InitalData();
List
citySource = source.Where(a => a.ProviceNode.ToString().Contains(proviceName)).First().citySource;
return Json(citySource, JsonRequestBehavior.AllowGet);
}
数据准备完毕,此时需要Razor视图中进行绑定和展示,代码如下:
代码如下:
@model MvcApplication.Models.DP_Provice
@{
ViewBag.Title = "DPShow";
Layout = "~/Views/Shared/_Layout.cshtml";
}