Search code examples
asp.netasp.net-mvcmodel-view-controlleractionresult

ASP.NET web application MVC how to pass data from a Get ActionResult to a Post ActionResult


I am trying to pass the searchString and sort variables from 1 ActionResult to the next.

 // 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);
        }

In the GET ActionResult I can print the searchString and sort variables and I can display them on the view like this:

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

However, for some reason the POST ActionResult has no idea what these variables are. I need them in the POST ActionResult because I need to again pass them to another ActionResult where they will finally be used.

How can I access the searchString and sort variables in the POST "Edit" ActionResult?


Solution

  • If you want to post those values you'll need to include them in your form with a name attribute that matches your post method parameter name, add two elements to your form:

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