【问题标题】:ASP.NET web application MVC how to pass data from a Get ActionResult to a Post ActionResultASP.NET Web 应用程序 MVC 如何将数据从 Get ActionResult 传递到 Post ActionResult
【发布时间】:2021-04-15 13:53:13
【问题描述】:

我正在尝试将 searchString 和排序变量从 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 searchString, 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 和排序变量,我可以像这样在视图上显示它们:

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

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

如何在 POST “Edit” ActionResult 中访问 searchString 和排序变量?

【问题讨论】:

    标签: asp.net asp.net-mvc model-view-controller actionresult


    【解决方案1】:

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

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

    【讨论】:

    • 我想过做这样的事情,但我认为没有办法做到这一点。好吧,谢谢你,这很有意义!
    • @ColePerry 不客气。乐意效劳。 :)
    猜你喜欢
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多