我不明白以下内容:
我的视图上有一个Ajax.ActionLink,它启动对控制器的ajax调用,以返回局部视图。 在下面的视图中,您可以看到我的Ajax.ActionLink方法。
这是我的观点(主页):
model EDR.ViewModels.ViewModels.PersonListViewModel
@{
ViewBag.Title = "Person";
}
<div class="row">
<div class="col-md-12">
<div id="partialPlaceHolder">
<h2>@ViewBag.Title</h2>
@*@using (Html.BeginForm("PersonListOptions", "Person", FormMethod.Post))*@
@using (Ajax.BeginForm("PersonListOptions", "Person", new AjaxOptions { HttpMethod = "post", InsertionMode = InsertionMode.Replace, UpdateTargetId = "updatedContent" }))
{
<p>
Search Criteria: @Html.TextBox("SearchString")
Search Options: @Html.DropDownList("SearchOptions", ViewBag.SearchOptions as IEnumerable<SelectListItem>)
<input type="submit" value="Search" class="btn btn-primary" />
</p>
}
<div id="updatedContent">
@{ Html.RenderPartial("_PersonListContent"); }
</div>
@Ajax.ActionLink("<<", "PersonListPaging", new { pageNumber = Model.PageNumber - 1, sortOption = Model.SortOption }, new AjaxOptions { HttpMethod = "get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "updatedContent" }, new { @class = "btn btn-info" })
@Ajax.ActionLink(">>", "PersonListPaging", new { pageNumber = Model.PageNumber + 1, sortOption = Model.SortOption }, new AjaxOptions { HttpMethod = "get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "updatedContent" }, new { @class = "btn btn-info" })
</div>
</div>
</div>
这是我的部分视图:
@model EDR.ViewModels.ViewModels.PersonListViewModel
@Styles.Render("~/Content/css")
<table class="table table-hover">
<thead>
<tr>
@*<th>@Html.ActionLink("Name", "PersonList", new { sort = "Name", sortDirection = Session["SortDirection"] as String })</th>*@
<th>@Ajax.ActionLink("Name", "PersonListOptions", new { sort = "Name", sortDirection = Session["SortDirection"] as String }, new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "get", UpdateTargetId = "updatedContent" })</th>
<th>@Ajax.ActionLink("Date of Birth", "PersonListOptions", new { sort = "BirthDate", sortDirection = Session["SortDirection"] as String }, new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "get", UpdateTargetId = "updatedContent" })</th>
<th>@Ajax.ActionLink("Last Reg Date", "PersonListOptions", new { sort = "LastActiveRegistrationDate_NDF", sortDirection = Session["SortDirection"] as String }, new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "get", UpdateTargetId = "updatedContent" })</th>
<th>@Ajax.ActionLink("Last Reg Number", "PersonListOptions", new { sort = "LastActiveRegistrationNo_NDF", sortDirection = Session["SortDirection"] as String }, new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "get", UpdateTargetId = "updatedContent" })</th>
<th>@Ajax.ActionLink("Reg Count", "PersonListOptions", new { sort = "ActiveRegistrationCount_NDF", sortDirection = Session["SortDirection"] as String }, new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "get", UpdateTargetId = "updatedContent" })</th>
<th>@Ajax.ActionLink("Gender", "PersonListOptions", new { sort = "GenderDescription_FKD", sortDirection = Session["SortDirection"] as String }, new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "get", UpdateTargetId = "updatedContent" })</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var person in Model.PersonList.Items)
{
<tr>
<td>@person.Name @person.Surname</td>
<td>@person.BirthDate</td>
<td>@person.LastActiveRegistrationDate_Description</td>
<td>@person.LastActiveRegistrationNo_Description</td>
<td>@person.ActiveRegistrationCount_Description</td>
<td>@person.Gender_Description</td>
<td>@Ajax.ActionLink("Edit/View", "EditPerson", "Person", new { id = person.ID }, new AjaxOptions { UpdateTargetId = "partialPlaceHolder", HttpMethod = "get", InsertionMode = InsertionMode.Replace }, new { @class = "btn btn-info btn-sm" })</td>
</tr>
}
</tbody>
</table>
这是我的控制器上的操作方法:
public PartialViewResult PersonListPaging(int pageNumber, string sortOption)
{
var vm = new PersonListViewModel
{
PageNumber = pageNumber == 0 ? 1 : pageNumber,
SortOption = sortOption
};
vm.RefreshList();
return PartialView("_PersonListContent", vm);
}
在上面的控制器操作中,我收到页码,然后使用当前收到的页码创建一个新VM并将其传递回视图。当我再次单击该按钮时,之前会传递相同的数字,并且在我的操作方法中尚未将视图模型更新为新创建的VM。看起来Model.PageNumber总是1,因为我继续在我的行动中获得pageNumber的值2。
非常感谢任何帮助
答案 0 :(得分:1)
您需要将寻呼逻辑移动到部分视图,因为每次转到下一页或上一页时都需要刷新页码。
所以你的主要观点应该是这样的。
model EDR.ViewModels.ViewModels.PersonListViewModel
@{
ViewBag.Title = "Person";
}
<div class="row">
<div class="col-md-12">
<div id="partialPlaceHolder">
<h2>@ViewBag.Title</h2>
@*@using (Html.BeginForm("PersonListOptions", "Person", FormMethod.Post))*@
@using (Ajax.BeginForm("PersonListOptions", "Person", new AjaxOptions { HttpMethod = "post", InsertionMode = InsertionMode.Replace, UpdateTargetId = "updatedContent" }))
{
<p>
Search Criteria: @Html.TextBox("SearchString")
Search Options: @Html.DropDownList("SearchOptions", ViewBag.SearchOptions as IEnumerable<SelectListItem>)
<input type="submit" value="Search" class="btn btn-primary" />
</p>
}
<div id="updatedContent">
@{ Html.RenderPartial("_PersonListContent"); }
</div>
</div>
</div>
</div>
部分观点应该是:
@model EDR.ViewModels.ViewModels.PersonListViewModel
@Styles.Render("~/Content/css")
<table class="table table-hover">
<thead>
<tr>
@*<th>@Html.ActionLink("Name", "PersonList", new { sort = "Name", sortDirection = Session["SortDirection"] as String })</th>*@
<th>@Ajax.ActionLink("Name", "PersonListOptions", new { sort = "Name", sortDirection = Session["SortDirection"] as String }, new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "get", UpdateTargetId = "updatedContent" })</th>
<th>@Ajax.ActionLink("Date of Birth", "PersonListOptions", new { sort = "BirthDate", sortDirection = Session["SortDirection"] as String }, new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "get", UpdateTargetId = "updatedContent" })</th>
<th>@Ajax.ActionLink("Last Reg Date", "PersonListOptions", new { sort = "LastActiveRegistrationDate_NDF", sortDirection = Session["SortDirection"] as String }, new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "get", UpdateTargetId = "updatedContent" })</th>
<th>@Ajax.ActionLink("Last Reg Number", "PersonListOptions", new { sort = "LastActiveRegistrationNo_NDF", sortDirection = Session["SortDirection"] as String }, new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "get", UpdateTargetId = "updatedContent" })</th>
<th>@Ajax.ActionLink("Reg Count", "PersonListOptions", new { sort = "ActiveRegistrationCount_NDF", sortDirection = Session["SortDirection"] as String }, new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "get", UpdateTargetId = "updatedContent" })</th>
<th>@Ajax.ActionLink("Gender", "PersonListOptions", new { sort = "GenderDescription_FKD", sortDirection = Session["SortDirection"] as String }, new AjaxOptions { InsertionMode = InsertionMode.Replace, HttpMethod = "get", UpdateTargetId = "updatedContent" })</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var person in Model.PersonList.Items)
{
<tr>
<td>@person.Name @person.Surname</td>
<td>@person.BirthDate</td>
<td>@person.LastActiveRegistrationDate_Description</td>
<td>@person.LastActiveRegistrationNo_Description</td>
<td>@person.ActiveRegistrationCount_Description</td>
<td>@person.Gender_Description</td>
<td>@Ajax.ActionLink("Edit/View", "EditPerson", "Person", new { id = person.ID }, new AjaxOptions { UpdateTargetId = "partialPlaceHolder", HttpMethod = "get", InsertionMode = InsertionMode.Replace }, new { @class = "btn btn-info btn-sm" })</td>
</tr>
}
</tbody>
</table>
@Ajax.ActionLink("<<", "PersonListPaging", new { pageNumber = Model.PageNumber - 1, sortOption = Model.SortOption }, new AjaxOptions { HttpMethod = "get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "updatedContent" }, new { @class = "btn btn-info" })
@Ajax.ActionLink(">>", "PersonListPaging", new { pageNumber = Model.PageNumber + 1, sortOption = Model.SortOption }, new AjaxOptions { HttpMethod = "get", InsertionMode = InsertionMode.Replace, UpdateTargetId = "updatedContent" }, new { @class = "btn btn-info" })