【发布时间】: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