ASP.NET Web 应用程序 MVC 如何将数据从 Get ActionResult 传递到 Post ActionResult

问题描述

我正在尝试将 searchString 和 sort 变量从 1 ActionResult 传递到下一个

 // GET: Case_Log/Edit/5
        public ActionResult Edit(int? id,string searchString,string sort)
        {
            System.Diagnostics.Debug.WriteLine("The search string was: " + searchString);
            System.Diagnostics.Debug.WriteLine("The sort string was: " + sort);
            ViewBag.CurrentSort = sort;
            ViewBag.CurrentFilter = searchString;

            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Case_Log case_Log = db.Case_Log.Find(id);
            if (case_Log == null)
            {
                return HttpNotFound();
            }
            return View(case_Log);
        }

        // POST: Case_Log/Edit/5
        // To protect from overposting attacks,enable the specific properties you want to bind to,for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,Name,Phone1,Phone2,Division_district,OrgNumber,DateOfTest,DateOfExposure,NumberOfExposed,Notes,PathToFile")] Case_Log case_Log,HttpPostedFileBase PostedFile,string sortOrder)
        {
            System.Diagnostics.Debug.WriteLine("The search string was: " + searchString);
            var currentPath = "";
            var currentPathQuery = from item in db.Case_Log
                                       where item.ID == case_Log.ID
                                       select item.PathToFile;

            foreach(var q in currentPathQuery)
            {
                currentPath = q;
            }

            if (PostedFile != null)
            {
                string path = Server.MapPath("~/Case_Log_Docs/");
                string fileName = Path.GetFileName(PostedFile.FileName);
                case_Log.PathToFile = fileName;
                PostedFile.SaveAs(path + fileName);
            }
            else
            {
                case_Log.PathToFile = currentPath;
            }

            if (ModelState.IsValid)
            {
                db.Entry(case_Log).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index",new { searchString = searchString,sortOrder = sortOrder});
            }
            return View(case_Log);
        }

在 GET ActionResult 中,我可以打印 searchString 和 sort 变量,并且可以像这样在视图中显示它们:

<h4>@ViewBag.CurrentFilter</h4>
<h4>@ViewBag.CurrentSort</h4>

然而,由于某种原因,POST ActionResult 不知道这些变量是什么。我需要在 POST ActionResult 中使用它们,因为我需要再次将它们传递给另一个 ActionResult,在那里它们最终会被使用。

如何访问 POST“编辑”ActionResult 中的 searchString 和排序变量?

解决方法

如果您想发布这些值,您需要将它们包含在表单中,并使用与您的 post 方法参数名称匹配的 name 属性,向表单中添加两个元素:

<form>
.....
<input type="hidden" name="searchString" value="@ViewBag.CurrentFilter" />
<input type="hidden" name="sortOrder" value="@ViewBag.CurrentSort" />

...
</form>