Mvc调用Api的显示、添加+二级联动
一、显示的DAL层代码
public ListLoadShow(string Dname, int? Cid, int? Eid, int? Ctid, int pageindex, int pagesize, out int totalcount, out int totalpage) { var query = from a in mm.DeliveryInfos join b in mm.Cities on a.Cid equals b.Cid where b.Cityid == 0 join c in mm.Cities on a.Ctid equals c.Cid where c.Cityid > 0 join d in mm.Expresses on a.Eid equals d.Eid select new ViewModel { Cid = b.Cid, Cname = b.Cname, Cityid = b.Cityid, Eid = d.Eid, Ename = d.Ename, Ctid = a.Ctid, Did = a.Did, Dname = a.Dname, ImgCp = a.ImgCp, Name = a.Name, Phone = a.Phone, Site = a.Site, State = a.State }; if (!string.IsNullOrWhiteSpace(Dname)) { query = query.Where(k => k.Dname.Contains(Dname)); } if (Cid != null) { query = query.Where(k => k.Cid == Cid); } if (Eid != null) { query = query.Where(k => k.Eid == Eid); } if (Ctid != null) { query = query.Where(k => k.Cityid == Ctid); } totalcount = query.Count(); totalpage = Convert.ToInt32(Math.Ceiling(totalcount * 1.0 / pagesize)); return query.OrderBy(k => k.Did).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList();
public int Add(DeliveryInfo d) { mm.DeliveryInfos.Add(d); return mm.SaveChanges(); }
三、二级联动的DAL层代码
public ListCityType(int Cityid) { return mm.Cities.Where(k => k.Cityid == Cityid).ToList(); }
四、显示的视图层代码Ajax方法
"pageinfo"> "#" onclick="page(‘F‘)">首页 "#" onclick="page(‘P‘)">上页 "#" onclick="page(‘N‘)">下页 "#" onclick="page(‘L‘)">尾页class="table table-bordered">
"background-color:coral;color:azure"> "8">产品发货管理 "8"> 产品名称 "text" id="Dname" /> 快递公司 <select id="Express"> select> <select id="Province" onchange="Shi()"> select> <select id="City"> select> "button" value="查询" class="btn btn-info" onclick="Load()"/> "button" value="添加" class="btn" style="background-color:coral;color:azure" /> "background-color:coral;color:azure"> "tblist">产品名称 收货人 收货人手机 收货地址 快递公司 图片 状态 操作
五、添加的视图层代码Ajax方法
function LodaAdd() { $.post(‘http://localhost:51198/api/Default/Add‘, { Dname: $("#Dname").val(), Name: $("#Name").val(), Phone: $("#Phone").val(), Cid: $("#Province").val(), Ctid: $("#City").val(), Site: $("#Site").val(), Eid: $("#Express").val(), ImgCp:"", State: $("[name=State]:checked").val() }, k => { if (k > 0) { alert("添加成功!"); location.href = "/Mvc/Index"; } }) }
六、二级联动的视图层代码
$(function () { Sheng(); Express(); }) function Sheng() { $("#Province").append("") $.get(‘http://localhost:51198/api/Default/CityType?Cityid=0‘, k => { $(k).each(function () { $("#Province").append("") }) }) } function Shi() { var id = $("#Province").val(); $("#City").empty(); $("#City").append("") $.get(‘http://localhost:51198/api/Default/CityType?Cityid=‘ + id, k => { $(k).each(function () { $("#City").append("") }) }) }
原文:https://www.cnblogs.com/713kk/p/15073524.html