我已经创建了一个人员登记表。在此表单中,我添加了一个显示人员列表的 partial view。我在这个表格上添加了一个分页。现在我想实现搜索功能。
我的表单如下所示
这里我想实现搜索功能。但问题是我在表单上有两个按钮,即 Create 和 search。当我点击搜索按钮时,它会为我提供表单 register.cshtml 的必填字段验证。
这是我的 register.cshtml
代码@model WebLess.Models.PersonModel
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.firstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.firstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.firstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.lastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.lastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.lastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
<div class="form-group">
@if (ViewBag.Message != null)
{
<span class="text-success">@ViewBag.Message</span>
}
</div>
<div class="form-group">
@Html.Partial("list", Model.listOfPerson)
</div>
</div>
}
这是我的list.cshtml。
@model PagedList.IPagedList<WebLess.Models.PersonModel>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@using (Html.BeginForm("Register", "Person", FormMethod.Get))
{
<p>
Find by name: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string)
<input type="submit" value="Search" />
</p>
}
<table class="table">
<tr>
<th>
First name
</th>
<th>
Last name
</th>
<th>
E-mail Address
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.firstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.lastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.email)
</td>
</tr>
}
</table>
<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Register",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
如何实现搜索功能?
请您参考如下方法:
您可以将列表部分 View 移到创建表单之外,然后将搜索功能放入列表部分 View 内它自己的表单中(无论如何它们是完全独立的,所以应该是独立的)。这应该会实现您正在寻找的东西。
