ASP.NET Web 应用程序 MVC 如何将数据从 Get ActionResult 传递到 Post ActionResult
ASP.NET web application MVC how to pass data from a Get ActionResult to a Post ActionResult
我正在尝试将 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“编辑”ActionResult 中的 searchString 和排序变量?
如果您想要 post 这些值,您需要将它们包含在您的表单中,并使用与您的 post 方法参数名称相匹配的名称属性,向您的表单添加两个元素:
<form>
.....
<input type="hidden" name="searchString" value="@ViewBag.CurrentFilter" />
<input type="hidden" name="sortOrder" value="@ViewBag.CurrentSort" />
...
</form>
我正在尝试将 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“编辑”ActionResult 中的 searchString 和排序变量?
如果您想要 post 这些值,您需要将它们包含在您的表单中,并使用与您的 post 方法参数名称相匹配的名称属性,向您的表单添加两个元素:
<form>
.....
<input type="hidden" name="searchString" value="@ViewBag.CurrentFilter" />
<input type="hidden" name="sortOrder" value="@ViewBag.CurrentSort" />
...
</form>