我们有一个显示数据行的索引页面。当我们去编辑一行时,Edit ActionResult 被点击并显示数据供我们编辑。然后当我们去提交更改时,HttpPost ActionResult 被命中并保存数据。
我们可以返回索引页面并查看保存的更改,但如果我们再次尝试编辑数据,则不会触发 Edit ActionResult 并且我们会看到旧数据,直到我们点击 F5,然后触发 Edit ActionResult 和dat 被刷新。
我们如何确保每次都点击 Edit ActionResult 而无需进行硬刷新?
谢谢!
这是控制器上的编辑 ActionResult:
[CustomAuthorizePDG]
public ActionResult Edit(int id = 0)
{
var model = this._db.ProductApprovals_ProductApproval.Find(id);
if (model == null) {
return HttpNotFound();
}
var spServer = ConfigurationManager.ConnectionStrings["SPServer"].ConnectionString;
ViewBag.ProductStatusId = new SelectList(this._db.ProductApprovals_ProductStatus, "ProductStatusId", "ProductStatus", model.ProductStatusId);
return View(model);
}
然后是 HttpPost AtionResult:
[CustomAuthorizePDG]
[HttpPost]
[ValidateAntiForgeryToken]
[ErrorHandler]
public ActionResult Edit(ProductApprovals_ProductApproval model, HttpPostedFileBase file)
{
if (ModelState.IsValid) {
if (file != null && file.ContentLength > 0) {
var sp = new ProductApprovalDataContext(new Uri("http://sp-appcentral-int/ProductApproval/_vti_bin/ListData.svc"))
{
Credentials = CredentialCache.DefaultNetworkCredentials
};
var productApprovalForm = sp.ProductApprovalForm.Where(x => x.ProductApprovalId == model.ProductApprovalId.ToString(CultureInfo.InvariantCulture)).FirstOrDefault();
if (productApprovalForm != null) {
var fileName = Path.GetFileName(file.FileName);
var extension = Path.GetExtension(file.FileName);
var name = string.Format("{0}{1}", model.ProductApprovalId, extension);
var path = string.Format("/ProductApproval/Product Approval Form/{0}", name);
var contentType = extension == "docx" ? "application/vnd.openxmlformats-officedocument.wordprocessingml.document" : "application/msword";
productApprovalForm.CheckedOutTo = new UserInformationListItem
{
UserName = User.Identity.Name
};
productApprovalForm.Title = fileName;
sp.SetSaveStream(productApprovalForm, file.InputStream, false, contentType, path);
sp.SaveChanges(SaveChangesOptions.ReplaceOnUpdate);
this.UpdateProductApprovalWithDocument(model, path, fileName);
}
}
this._db.Entry(model).State = EntityState.Modified;
this._db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ProductStatusId = new SelectList(this._db.ProductApprovals_ProductStatus, "ProductStatusId", "ProductStatus", model.ProductStatusId);
return View(model);
}
因此,当触发 HttpPost Edit 时,它会成功保存更改并显示在索引视图中。如果您随后返回到 Edit ActionResult,您将看到初始值,直到您执行刷新。我们在代码上设置了一个断点,第二次编辑 ActionResult 直到你按下 F5 才被触发......