简体   繁体   English

在部分视图上的mvc4分页

[英]mvc4 paging on partial view

I have created a person register form. 我已经创建了人事登记表。 In this form I have added one partial view which shows list of persons. 在此表单中,我添加了一个显示人员列表的partial view I have added a paging on this form. 我在此表单上添加了分页。 Now I want to implement search functionality. 现在,我想实现搜索功能。

My form looks like following 我的表格如下

在此处输入图片说明

Here I want to implement search functionality. 在这里,我想实现搜索功能。 but the issue is I have two buttons on a form ie Create and search . 但问题是我在表单上有两个按钮,即“ Create和“ 搜索” when I click on search button it gives me required field validation of form register.cshtml . 当我单击搜索按钮时,它为我提供了register.cshtml格式的必填字段验证。

Here is my code for 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>
}

Here is my list.cshtml . 这是我的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 }))

How can I implement search functionality? 如何实现搜索功能?

You can move the list partial view outside of the creation form, then put the search functionality into its own form inside the list partial view (they're completely separate anyway, so should be separate). 您可以将列表部分视图移到创建表单之外,然后将搜索功能放入列表部分视图内的自己的表单中(它们还是完全分开的,因此应该分开)。 This should then achieve what you're looking for. 这样就可以实现您想要的。

Nesting of forms is not allowed and may result in an undefined behavior between browsers. 不允许嵌套表格 ,这可能导致浏览器之间出现不确定的行为。

Use two seperate form instead of nested form 使用两个单独的形式而不是嵌套形式

like steve says "move the list partial view outside of the creation form" would result into two separate form instead of one form inside another form 就像史蒂夫(Steve)所说的“将列表局部视图移到创建表单之外”会导致分成两个单独的表单,而不是另一个表单中的一个表单

like 喜欢

@using (Html.BeginForm())
{
}

@using (Html.BeginForm("Register", "Person", FormMethod.Get))
{
}

instead of ( a form in a form is not allowed ) 代替( 不允许表单中的表单

 @using (Html.BeginForm())
    {

       @using (Html.BeginForm("Register", "Person", FormMethod.Get))
       {
       }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM