第一次写博客见谅见谅
MVC可能是现在常用的框架之一
先是普通的显示出来
MVC块的代码
作用:就收前台的数据,导入DAL。
using DAL; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Data.SqlClient; namespace MvcApplication1.Controllers { public class PhoneController : Controller { // // GET: /Phone/ public ActionResult Phones() { UserDAL ud = new UserDAL(); /******查询******/ //获取用户传递来的数据 string cxname = Request["cxname"]; //DAL返回的数据 List<Users> ls = ud.postusers(cxname); ViewBag.ls = ls; //实现查询框不更新,返回参数到前台 ViewBag.cxname = cxname; return View(); } } }
DAL块的代码
作用:连接数据库
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; namespace DAL { /// <summary> /// 查询用户 /// </summary> /// <param name="cxname"></param> /// <returns></returns> public List<Users> postusers(string cxname) { OAEntities oae = new OAEntities(); //序列化数据库字段, List<Users> ulist = oae.Users.ToList(); //模糊查询,如果前台没有查询就显示全部 ulist = oae.Users.Where(a => (a.UserName.Contains(cxname)||a.UserPhone.Contains(cxname) ||a.UserQQ.Contains(cxname)||a.UserMail.Contains(cxname) ||a.UserAdd.Contains(cxname)||string.IsNullOrEmpty(cxname)/*cxname为空*/)).ToList(); return ulist; } }
前台的代码
jQuery上的代码
<script> $(function () { //先找到父级,不用管,样式 var partent = $(".page-sidebar-menu > li").eq(4).addClass("active"); //选择子菜单 partent.find("li").eq(0).addClass("active"); //接收查询的数据 $("#cx").click(function () { var cxname = $("#cxtxt").val(); //把数据传输到后台位置 location.href = "/Phone/phones?cxname=" + cxname; }); }); </script> ``` HTML的代码 作用:显示出来,接收用户输入的字符 ```python <div class="portlet-body"> <input id="cxtxt" type="text" placeholder="请输入查询的用户名" class="m-wrap medium2" @*不会因为查找而更新*@value="@ViewBag.cxname"> <button type="button" class="btn blue" id="cx">查询</button> <table class="table table-striped table-hover"> <thead> <tr> <th>ID</th> <th>姓名</th> <th>电话</th> <th>QQ</th> <th>邮箱</th> <th>地址</th> </tr> </thead> <tbody> @{ List<Users> ulist = ViewBag.ls as List<Users>; foreach (Users item in ulist) { <tr> <td>@item.Id</td> <td>@item.UserName</td> <td>@item.UserPhone</td> <td>@item.UserQQ</td> <td>@item.UserMail</td> <td>@item.UserAdd</td> </tr> } } </tbody> </table> </div>
查询全部的效果图
查询字段的效果图
新手学习中有问题希望大家评论一下,会改