一、 WebAPI+MVC问题汇总
1. 未生成解决方案 (写完Model之后 未编译) 导致 迁移失效
2. API部门未改路由 未添加{action} 路由
3. 数据迁移的时候 未选择 上下文所在的 那个类库
4. 跨域配置的时候 未引用
5. WEBAPI UI 层 未引用 EF
二、招聘,WebAPI的dal层
/// <summary> /// 显示 分页 查询 /// </summary> /// <param name="totalcount"></param> /// <param name="totalpage"></param> /// <param name="categoryId"></param> /// <param name="depqrtmentId"></param> /// <param name="pageindex"></param> /// <param name="pagesize"></param> /// <returns></returns> public List<JobViewModel> GetShow(out int totalcount,out int totalpage,int? categoryId,int? depqrtmentId,int pageindex=1,int pagesize=3) { var query = from a in db.UserModels join b in db.OnifModels on a.CategoryId equals b.Id where b.Code == "type" join c in db.OnifModels on a.DepartmentId equals c.Id where c.Code == "dept" join d in db.OnifModels on a.CityId equals d.Id where d.Code == "city" select new JobViewModel { ID = a.ID, CategoryId = a.CategoryId, CategoryName = b.Tname, DepartmentId = a.DepartmentId, DepartmentName = c.Tname, CityId = a.CityId, CityName = d.Tname, Name = a.Name, Salary = a.MinSalary + "--" + a.MaxSalary, Type = a.Type, Wanted = a.Wanted, Time = a.Time }; if(categoryId!=null) { query = query.Where(p => p.CategoryId == categoryId); } if(depqrtmentId!=null) { query = query.Where(p => p.DepartmentId == depqrtmentId); } totalcount = query.Count(); totalpage = Convert.ToInt32(Math.Ceiling(totalcount * 1.0 / pagesize)); return query.OrderBy(p => p.ID).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList(); } /// <summary> /// 批量删除 /// </summary> /// <param name="Ids"></param> /// <returns></returns> public int DelAll(string Ids) { try { var arr = Ids.Split(','); foreach (var item in arr) { var id = Convert.ToInt32(item); var obj = db.UserModels.FirstOrDefault(p => p.ID == id); if(obj!=null) { db.UserModels.Remove(obj); } } return db.SaveChanges(); } catch (Exception) { throw; } } /// <summary> /// 单删 /// </summary> /// <param name="id"></param> /// <returns></returns> public int Delete(int id) { try { var list= db.UserModels.FirstOrDefault(p => p.ID == id); if(list!=null) { db.UserModels.Remove(list); return db.SaveChanges(); } else { return 0; } } catch (Exception) { throw; } } /// <summary> /// 编译 /// </summary> /// <param name="id"></param> /// <returns></returns> public UserModel FanTian(int id) { return db.UserModels.FirstOrDefault(p => p.ID == id); } /// <summary> /// 修改 /// </summary> /// <param name="id"></param> /// <returns></returns> public int Update(UserModel id) { try { db.Entry(id).State = System.Data.Entity.EntityState.Modified; return db.SaveChanges(); } catch (Exception) { throw; } } /// <summary> /// 绑定下拉框 /// </summary> /// <param name="code"></param> /// <returns></returns> public List<OnifModel> GetXiaLa(string code) { return db.OnifModels.Where(p => p.Code == code).ToList(); } /// <summary> /// 登录 /// </summary> /// <param name="uname"></param> /// <param name="upass"></param> /// <returns></returns> public DengLu DengLut(string uname,string upass) { return db.DengLus.Where(p => p.Name == uname && p.MimMa == upass).FirstOrDefault(); }
三、Mvc显示+分页+查询操作使用方法ajax
@{ ViewBag.Title = "Index"; } <script src="~/Scripts/jquery-3.4.1.min.js"></script> <link href="~/Content/bootstrap.css" rel="stylesheet" /> <h2>当前登录名为<span id="uname"></span></h2> <div> 类目<select id="categoryId"> <option>请选择</option> </select> 所属部门<select id="depqrtmentId"> <option>请选择</option> </select> <input type="button" value="搜索" onclick="GetShow()"/> <input type="button" value="批量删除" onclick="PiLiang()"/> </div> <table class="table table-bordered"> <thead> <tr> <th><input type="checkbox" id="All"/></th> <th>序号</th> <th>招牌类型</th> <th>类目</th> <th>所属部门</th> <th>岗位名称</th> <th>工作地点</th> <th>是否急聘</th> <th>薪资范围/月</th> <th>创建时间</th> <th>操作</th> </tr> </thead> <tbody id="list"> </tbody> </table> <div> <span id="XianYe"> </span> <select id="YeShu" onchange="YeShu()"> <option value="1">1条/页</option> <option value="2">2条/页</option> <option value="3" selected>3条/页</option> <option value="4">4条/页</option> </select> <a href="#" onclick="Page('F')">首页</a> <a href="#" onclick="Page('P')">上一页</a> <a href="#" onclick="Page('N')">下一页</a> <a href="#" onclick="Page('L')">尾页</a> 前往 <input id="TiaoZHuan" type="text" style="width:50px"/> 页 <input type="button" value="确定" onclick="Tiao()"/> </div> <script> pageindex = 1; pagesize = 3; totalcount = 0; totalpage = 0; //文档就绪函数 $(function () { $("#uname").text(localStorage.getItem("uname")) GetShow(); QuanXuan(); LoadSelect('categoryId', 'type'); LoadSelect('depqrtmentId', 'dept'); }) //加载数据 function GetShow() { $.get('http://localhost:49557/api/User/GetShow', { //?categoryId=& depqrtmentId=& pageindex=1 & pagesize=3 categoryId: $("#categoryId").val(), depqrtmentId: $("#depqrtmentId").val(), pageindex: pageindex, pagesize: pagesize }, res => { var number = 1; //给总条数进行赋值 totalcount = res.totalcount; totalpage = res.totalpage; $("#list").empty(); $(res.list).each(function () { var arr = '<tr>' + '<td><input type="checkbox" name="box" value="' + this.ID + '" /></td>' + '<td>' + number + '</td>' + '<td>' + this.Type+ '</td>' + '<td>' + this.CategoryName+ '</td>' + '<td>' + this.DepartmentName+ '</td>' + '<td>' + this.Name+ '</td>' + '<td>' + this.CityName+ '</td>' + '<td>' + this.Wanted+ '</td>' + '<td>' + this.Salary+ '</td>' + '<td>' + this.Time + '</td>' + '<td><a href="Enid?id=' + this.ID + '">编辑</a>|<a href="#" name="CNam" onclick="Delete(' + this.ID + ')" >删除</a></td>' + '</tr>' $("#list").append(arr); number++; }) $("#XianYe").html('当前' + pageindex + '/' + totalpage + '共' + totalcount + '条').val(); }) } //加载菜单 function LoadSelect(id, code) { $.get('http://localhost:49557/api/User/GetXiaLa?code='+code+'', res => { $(res).each(function () { $("#" + id).append('<option value="' + this.Id + '">' + this.Tname+'</option>') }) }) } //分页 function Page(o) { switch (o) { case 'F': pageindex = 1; break; case 'P': pageindex = pageindex <= 1 ? pageindex : pageindex - 1; break; case 'N': pageindex = pageindex >= totalpage ? pageindex : pageindex + 1; break; case 'L': pageindex = totalpage; break; } GetShow(); } //显示条数 function YeShu() { pagesize = $("#YeShu").val(); GetShow(); } //跳转 function Tiao() { pageindex = $("#TiaoZHuan").val(); GetShow(); } //全选/非全选 function QuanXuan() { $("#All").click(function () { $('[name=box]').prop("checked", this.checked); }) } //批量删除 function PiLiang() { var arr = []; $('[name=box]:checked').each(function () { arr.push(this.value); }) if (arr.length === 0) { alert("请选择将要删除的数据"); return; } if (confirm("确定删除吗")) { $.post('http://localhost:49557/api/User/DelAll?Ids=' + arr.toString(), res => { if (arr > 0) { alert("删除成功"); GetShow(); } }) } } //单删 function Delete(id) { if (confirm("确定删除吗?")) { $.post('http://localhost:49557/api/User/Delete/'+id+'', res => { if (res > 0) { alert("删除成功"); GetShow(); } }) } } //编辑 </script>
四、Mvc编译修改 操作使用方法ajax
@{ ViewBag.Title = "Enid"; } <script src="~/Scripts/jquery-3.4.1.min.js"></script> <link href="~/Content/bootstrap.css" rel="stylesheet" /> <h2>Enid</h2> <table class="table table-bordered"> <tr> <td> 招聘类型: <input type="text" id="Type" /> </td> <td> 工作城市: <select id="City"></select> </td> </tr> <tr> <td> 类目: <select id="categoryId"></select> </td> <td> 是否急聘: <input type="radio" id="Wanted" name="Wanted" value="true" />是 <input type="radio" id="Wanted" name="Wanted" value="false" />否 </td> </tr> <tr> <td> 用人部门: <select id="depqrtmentId"></select> </td> <td> 薪资范围: <input type="text" style="width:50px" id="MaxSalary" /> - <input type="text" style="width:50px" id="MinSalary" /> <select id="Timet"> <option value="元/年">元/年</option> <option value="元/月">元/月</option> </select> <input type="checkbox" id="MYi"/> </td> </tr> <tr> <td> 职位名称: <input type="text" id="Name" /> </td> <td></td> </tr> <tr> <td> 请描述该岗位的基础信息 <input type="text" id="Information" /> </td> <td></td> </tr> <tr> <td colspan="2"> <input type="button" value="编辑" onclick="Update()" /> </td> </tr> </table> <script> //截取ID var ID = location.search.substring(4); $(function () { LoadSelect('categoryId', 'type'); LoadSelect('depqrtmentId', 'dept'); LoadSelect('City', 'city') BianJi(); }) //加载菜单 function LoadSelect(id, code) { $.get('http://localhost:49557/api/User/GetXiaLa', { code: code }, res => { $(res).each(function () { $("#" + id).append('<option value="' + this.Id + '">' + this.Tname + '</option>') }) }) } //编译 function BianJi() { $.get('http://localhost:49557/api/User/FanTian/' + ID, res => { $("#Type").val(res.Type); $("#categoryId").val(res.CategoryId); $("#depqrtmentId").val(res.DepartmentId); $("#Name").val(res.Name); $("#City").val(res.CityId); $("[name=Wanted][value=" + res.Wanted + "]").prop("checked", true); $("#MaxSalary").val(res.MaxSalary); $("#MinSalary").val(res.MinSalary); $("#Timet").val(res.SalaryTime); $("#MYi").prop("checked", res.Negotiable); $("#Information").val(res.Information) }) } //修改 function Update() { $.post('http://localhost:49557/api/User/Update', { ID: ID, Type: $("#Type").val(), CategoryId: $("#categoryId").val(), DepartmentId: $("#depqrtmentId").val(), Name: $("#Name").val(), CityId: $("#City").val(), Wanted: $("[name=Wanted]:checked").val(), MaxSalary: $("#MaxSalary").val(), MinSalary: $("#MinSalary").val(), SalaryTime: $("#Timet").val(), Negotiable: $("[name=MYi]:checked").val(), Information: $("#Information").val() }, res => { if (res > 0) { location.href = 'Index'; } else { alert("修改失败"); } }) } </script>
五、登录
@{ ViewBag.Title = "DengLu"; } <script src="~/Scripts/jquery-3.4.1.min.js"></script> <h2>DengLu</h2> <table class="table table-bordered"> <tr> <td>用户名</td> <td><input type="text" id="uname" name="name" value="" /></td> </tr> <tr> <td>密码</td> <td><input type="password" id="upass" name="name" value="" /></td> </tr> </table> <input type="button" name="name" value="登录" onclick="Login()" /> <script> function Login() { var uname = $("#uname").val(); var upass = $("#upass").val(); if (uname == '' || upass == '') { alert('请输入用户名和密码'); return; } $.get('http://localhost:49557/api/User/DengLut?uname=' + uname + '&upass=' + upass+ '', res => { if (res != null) { localStorage.setItem("uname", res.Name) location.href = 'index' } else { alert('登录失败'); } }) } </script>
待续......