微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ASP.NET MVC 如何将数据从 GET ActionResult 传递到同一视图的 POST ActionResult

如何解决ASP.NET MVC 如何将数据从 GET ActionResult 传递到同一视图的 POST ActionResult

我有这些创建 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);
        }

我需要将值 fullName 从 GET 方法传递到 POST 方法。有几篇帖子询问如何将数据从一个动作结果传递到另一个动作结果,但我还没有见过这样的。我不知道如何将数据从 GET Create 传递到 POST Create 或如何在发送后检索数据。

解决方法

当您在 post 方法中需要数据时,您无法从 get 方法发送或检索数据,因为 Get 方法已完成执行并被清理。我建议您在发布请求时在要发布的页面上添加一个隐藏字段,或者重新获取发布请求中的数据。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。