我們有一個顯示數據行的索引頁面。當我們去編輯一行時,Edit ActionResult會被點擊並顯示數據供我們編輯。然後當我們去提交更改時,HttpPost ActionResult被命中並且數據被保存。MVC 4 ActionResult未被第二次擊中
我們可以返回到索引頁面並查看已保存的更改,但是如果我們再次嘗試編輯數據,則不會觸發Edit ActionResult,並且會顯示舊數據,直到我們點擊F5,然後觸發編輯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 ActionResult,則會顯示初始值,直到執行刷新。我們在代碼上放置了一個斷點,第二次編輯ActionResult並沒有被觸發,直到你點擊F5 ...
你的代碼在哪裏?我認爲沒有代碼,沒有人可以幫助你... – MRB