我有一个项目列表。我在这里使用分页显示10个项目/页面工作正常。但是,当我"检查"这个项目,我更改了"已检查"数据库中的值来自' false'真的'。当商品的状态为“真实”时它不应该再出现在列表中。更改此值也可以正常工作,但我的问题在于分页。它通过从列表中删除项目来更新,这很好,但现在第一页上不再有10个项目,而是9.这一直持续到第一页为空,我必须切换到第2页继续检查项目,这显然不是很好。我的分页错误是什么?
我在视图中的列表:
@using PagedList.Mvc
@model PagedList.IPagedList<WebApplication1.Models.ErrorModel>
<ul class="list-group">
@foreach (var item in Model)
{
if (item.Checked == "False")
{
<li class="list-group-item">
<div class="row">
<div class="col-md-4">
<h4>
Instrument ID: @item.InstrumentId
</h4>
<h4 id="checkBtn">@Html.ActionLink("Go to instrument", "Instrument", new {instrumentid = item.InstrumentId})</h4>
Not yet checked: <button type="button" class="open-dialog btn btn-primary btn-sm" data-toggle="modal" data-target="#myModal" data-url="@Url.Action("CheckError", new {isChecked = item.Checked, error = item.InstrumentId})">Check</button>
</div>
<div class="col-md-4">
@item.Type
</div>
<div class="col-md-4">
@item.Message
</div>
</div>
</li>
}
}
按钮打开的对话框:
<!-- Remove from error list-Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Do you want to remove this error from the error list?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<a id="error" class="btn btn-primary btn-sm" href="#">Yes, save changes</a>
</div>
</div>
</div>
<script type="text/javascript">
$(document).on("click", ".open-dialog", function() {
$('#error').attr('href', $(this).data('url')); // update the link's url
});
</script>
这是控制器:
public ActionResult CheckError(string error)
{
string cs = "Data Source=" + "some url";
using (SQLiteConnection con = new SQLiteConnection(cs))
{
string stm = "UPDATE Error SET Checked = 'True' WHERE InstrumentID = " + "'" + error + "'";
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
{
cmd.CommandText = stm;
cmd.ExecuteNonQuery();
}
con.Close();
}
return RedirectToAction("Error");
}
为了更好的衡量,控制器被重定向到:
public ActionResult Error(int? page)
{
string cs = "Data Source=" + "some url";
using (SQLiteConnection con = new SQLiteConnection(cs))
{
var listOfErrors = new List<ErrorModel>();
string stm = "SELECT * FROM Error";
con.Open();
using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
{
using (SQLiteDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
listOfErrors.Add(new ErrorModel
{
Checked = rdr["Checked"].ToString(),
});
}
rdr.Close();
ErrorList = listOfErrors;
}
}
con.Close();
}
// stuff for paging
int pageSize = 10;
int pageNumber = (page ?? 1); // if there is no page, return page 1
return View(ErrorList.ToPagedList(pageNumber, pageSize));
}
答案 0 :(得分:1)
您正在过滤客户端,这不是一个好主意。你从数据库中获取所有记录,然后分页集返回第一个10.在客户端,你只是忽略了检查的任何一个。第一页是原始的10,只是忽略了一些行。您只需要返回要显示的记录。
你不能改变Error动作的SQL,并添加WHERE Checked == false吗?这也会更快,更有效率。