Ajax在MVC中实现模糊查询
控制器里:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AjaxDemo.Models;
namespace AjaxDemo.Controllers
{
public class HomeController : Controller
{
JobDBEntities db =new JobDBEntities();
public ActionResult Index()
{
List<JobSeeker> list = db.JobSeekers.ToList();
//先生成数据表对应的模型
return View(list);
}
public ActionResult select(string UserName)
{
List<JobSeeker> list = db.JobSeekers.Where(u => u.Name.Contains(UserName)).ToList();
return PartialView("result", list);
}
public ActionResult result()
{
return View();
}
}
}
result视图的前端代码:
@model List<AjaxDemo.Models.JobSeeker>
//通过EF模型:Model→新建项
<div>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>姓名</th>
<th>求职意向</th>
<th>工作经验</th>
<th>教育程度</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.Name</td>
<td>@item.CareerObjective</td>
<td>@item.Experience</td>
<td>@item.Education</td>
</tr>
}
</tbody>
</table>
</div>
Index视图的前端代码:
@model List<AjaxDemo.Models.JobSeeker>
@using (Ajax.BeginForm("select", "Home", new AjaxOptions
{
Confirm = "确定注册吗?",
HttpMethod = "Post",
InsertionMode = InsertionMode.Replace,
//InsertBefore,在刷新数据区域,原来基础的前面进行插入
LoadingElementId = "loading",
UpdateTargetId = "show",
}))
{
<table>
<tr>
<td>查询:</td>
<td><input type="text" name="UserName"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="查询"/></td>
</tr>
</table>
}
<div id="show">
<div>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>姓名</th>
<th>求职意向</th>
<th>工作经验</th>
<th>教育程度</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.Name</td>
<td>@item.CareerObjective</td>
<td>@item.Experience</td>
<td>@item.Education</td>
</tr>
}
</tbody>
</table>
</div>
</div>