【发布时间】:2020-04-12 16:08:13
【问题描述】:
我能够使用 Entity Framework 在 ASP.NET MVC 中通过搜索和分页创建 CRUD 操作,现在我想使用 Ajax 进行搜索和分页。
创建它的步骤是什么?
这是我的代码:
客户控制器
using MvcCRUDSearching.Models;
using PagedList;
namespace MvcCRUDSearching.Controllers
{
public class CustomerController : Controller
{
// GET: Customer
public ActionResult Index(string searchBy, string searchString, int? page)
{
using (DbModels dbModel = new DbModels())
{
if (searchBy == "Department")
{
IPagedList<Customer> dep = dbModel.Customers.Where(x => x.Department == searchString || searchString == null).ToList().ToPagedList(page ?? 1, 2);
return View(dep);
}
else if (searchBy == "Name")
{
IPagedList<Customer> nam = dbModel.Customers.Where(y => y.Name.StartsWith(searchString) || searchString == null).ToList().ToPagedList(page ?? 1, 2);
return View(nam);
}
else
{
return View(dbModel.Customers.ToList().ToPagedList(page ?? 1, 2));
}
}
}
}
}
Index.cshtml
@using MvcCRUDSearching.Models;
@using PagedList.Mvc;
@using PagedList;
@model IPagedList<MvcCRUDSearching.Models.Customer>
@{
ViewBag.Title = "Index";
}
<div id="div1">
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<p>
@using (@Html.BeginForm("Index", "Customer", FormMethod.Get))
{
<b> Search By:</b>
@Html.RadioButton("searchBy", "Name", true)<text>Name</text>
@Html.RadioButton("searchBy", "Department")<text>Department</text>
@Html.TextBox("searchString") <input type="submit" value="Search" class="btn" />
}
</p>
<table class="table">
<tr>
<th>
Name
</th>
<th>
Department
</th>
<th>Action</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Department)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
@Html.ActionLink("Details", "Details", new { id = item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.ID })
</td>
</tr>
}
</table>
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, searchBy = Request.QueryString["searchBy"], search = Request.QueryString["searchBy"] }))
</div>
我只知道我需要一个局部视图,但是我应该把它放在什么地方?
~/Views/Shared
或
~/Views/Customer
很抱歉问了这么长的问题,但我有点绿。
【问题讨论】:
-
如果您的局部视图在更棕褐色的视图中使用,则将其共享,否则放入特定的模块视图文件夹,如客户
-
好的,对不起,如果我听起来很愚蠢,但我应该使用列表模型吗?
标签: jquery ajax asp.net-mvc entity-framework asp.net-mvc-partialview