asp.net-mvcactionresult

ASP.NET MVC How to pass data from a GET ActionResult to the POST ActionResult of the same view


I have these create ActionResults:

   // GET: WC_Inbox/Create
        public ActionResult Create(int? id)
        {
            System.Diagnostics.Debug.WriteLine("Employee ID was: " + id);
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Employee employee = db.Employees.Find(id);
            if (employee == null)
            {
                return HttpNotFound();
            }
            string fullName = employee.First_Name + " " + employee.Last_Name;
            System.Diagnostics.Debug.WriteLine("Employee full name: " + fullName);
            ViewBag.EmployeeID = id;
            ViewBag.Name = fullName;
            ViewBag.Status = "Pending";
            return View();
        }

        // POST: WC_Inbox/Create
        // 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 Create([Bind(Include = "ID,InboxID,EmployeeID,Org_Number,Hire_Date,Job_Title,Work_Schedule,Injury_Date,Injury_Time,DOT_12,Start_Time,Injured_Body_Part,Side,Missing_Work,Return_to_Work_Date,Doctors_Release,Treatment,Injury_Description,Equipment,Witness,Questioned,Medical_History,Inbox_Submitted,Comments,User_Email,Contact_Email,Specialist_Email,Optional_Email,Optional_Email2,Optional_Email3,Optional_Email4,Status,Add_User,Date_Added")] WC_Inbox wC_Inbox)
        {
            if (ModelState.IsValid)
            {
                //Get logged in windows user, get the date right now, and create the wcInbox unique ID
                var userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                wC_Inbox.Add_User = userName;
                wC_Inbox.Date_Added = DateTime.Today;
                db.WC_Inbox.Add(wC_Inbox);
                db.SaveChanges();
                SendEmailIQ();
                return RedirectToAction("Index");
            }

            ViewBag.EmployeeID = new SelectList(db.Employees, "ID", "First_Name", wC_Inbox.EmployeeID);
            return View(wC_Inbox);
        }

I need to pass the value fullName from the GET method to the POST method. There are several posts asking how to pass data from one action result to another, but I haven't seen one like this. I don't know how to pass the data from GET Create to POST Create or how to retrieve data once it is sent.


Solution

  • You can't send or retrieve data from your get method at the point that you are needing it in the post method due to the fact that the Get method has finished executing and been cleaned up. I would advise you to either add a hidden field on the page to be posted when the post request is made or to re-fetch the data in the post request.