【问题标题】:MVC 4 ActionResult not being hit second time roundMVC 4 ActionResult 没有被第二次击中
【发布时间】:2013-10-23 13:22:48
【问题描述】:

我们有一个显示数据行的索引页面。当我们去编辑一行时,Edit ActionResult 被点击并显示数据供我们编辑。当我们然后去提交更改时,HttpPost ActionResult 被命中并保存数据。

我们可以返回索引页面并查看保存的更改,但如果我们再次尝试编辑数据,则不会触发 Edit ActionResult 并且我们会看到旧数据,直到我们点击 F5 然后触发 Edit ActionResult 并刷新数据。

我们如何确保每次都点击 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,您将看到初始值,直到您执行刷新。我们在代码上设置了一个断点,第二轮 Edit ActionResult 直到你按下 F5 才被触发...

【问题讨论】:

  • 您的代码在哪里?我认为没有代码没有人可以帮助你......

标签: asp.net-mvc-4 refresh actionresult


【解决方案1】:

在您的 HttpPost Edit 操作中,确保您正在执行 RedirectToAction 返回到 Index 操作。形成您的问题描述,如果您正在从编辑操作显示保存编辑视图。您将希望重定向回索引操作。

如果这仍然不起作用,请检查您的 Index 视图的 HTML 源代码,以确保您的编辑 url 仍然引用编辑操作方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多