【问题标题】:ASP.NET MVC How to pass data from a GET ActionResult to the POST ActionResult of the same viewASP.NET MVC 如何将数据从 GET ActionResult 传递到同一视图的 POST ActionResult
【发布时间】:2021-06-23 15:05:21
【问题描述】:

我有这些创建 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 或如何在发送数据后检索数据。

【问题讨论】:

  • 你能不能再确定一点?我看不到您试图在哪里传递值全名。您能否发布您的 Get 和 Post 方法以及您将如何通过它。
  • get 和 post 方法都已经给出......它们在问题中大声笑。而我的问题就是“如何通过它”。
  • 我仍然看不到需要通过什么。

标签: asp.net-mvc actionresult


【解决方案1】:

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

【讨论】:

  • 啊,这很有道理。在这种情况下,我将只做隐藏字段。我曾想过这样做,但这似乎不是正确的做法。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多