我正在使用PagedList
查看索引上的记录// GET: Product
public ActionResult Index(int page = 1)
{
int recordsPerPage = 1; //Page size = 1 record
var productList = db.q_product.ToList().ToPagedList(page, recordsPerPage);
return View(productList);
}
查看页面上的记录时,字段可编辑,如下图所示
我尝试创建的功能是能够编辑任何字段并单击“保存”,这将依次更新数据库中的记录,并在发布/单击保存事件后将其返回到同一记录。
<div class="form-group" style="float: left;">
<input type="submit" value="Save" class="btn btn-default" />
</div>
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Save(q_product q_product, int page = 1)
{
if (!ModelState.IsValid)
{
int recordsPerPage = 1;
var productList = db.q_product.ToList().ToPagedList(page, recordsPerPage);
return View(productList);
}
else
{
db.Entry(q_product).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
}
我如何实现此功能?
编辑:
记录采用以下代码所示的形式
<h4>PRODUCT VIEW</h4>
@foreach (var item in Model)
{
using (Html.BeginForm("Save", "Product"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.FirstOrDefault().q_guid)
<div class="panel panel-default">
<div class="panel-heading">PRODUCT INFORMATION</div>
<div class="row">
<div class="col-lg-4">
<div class="panel-body">
<div class="form-group">
<dt>
@Html.DisplayNameFor(model => model.FirstOrDefault().q_barcode)
</dt>
<dd>
@Html.EditorFor(modelItem => item.q_barcode)
</dd>
</div>
</div>
</div>
<div class="col-lg-4">
<div class="panel-body">
<div class="form-group">
<dt>
@Html.DisplayNameFor(model => model.FirstOrDefault().q_description)
</dt>
<dd>
@Html.EditorFor(modelItem => item.q_description)
</dd>
</div>
</div>
</div>