Asp.net mvc 在MVC中的HTTPPOST之后返回搜索结果
我有搜索表单要搜索:站点、用户、状态和日期。搜索后,我将拒绝、重新提交或批准调用控制器操作以更新数据库中的状态。我的代码如下: ///视图:Asp.net mvc 在MVC中的HTTPPOST之后返回搜索结果,asp.net-mvc,Asp.net Mvc,我有搜索表单要搜索:站点、用户、状态和日期。搜索后,我将拒绝、重新提交或批准调用控制器操作以更新数据库中的状态。我的代码如下: ///视图: @Html.ValidationMessage("CustomError") @Html.ValidationSummary(true) { ///HttpPost动作 [HttpPost, ActionName("POHeader")] [MultiButton(MatchFormKey = "action", MatchFormValue =
@Html.ValidationMessage("CustomError")
@Html.ValidationSummary(true)
{
///HttpPost动作
[HttpPost, ActionName("POHeader")]
[MultiButton(MatchFormKey = "action", MatchFormValue = "Reject")]
public ActionResult Reject(int[] selectedList)
{
string var1 = collection["sites"];
UpdateListStatus(selectedList, "X", "Rejected");
TempData["CustomError"] = selectedList.Count().ToString() + " record(s) has been successfully " + ActionString + "!";
return RedirectToAction("POHeader", new { IsRedirectAction = true });
}
问题:如何获取搜索值?
1.我是否需要在HTTPPost操作中再次传递所有参数?(有更好的解决方法吗?)
2.CustomError消息不起作用。(在下一次搜索完成后显示消息)
谢谢,
Si Thu首先,我会将视图绑定到一个模型。通过这样做,您可以将整个模型传递回您的方法,然后如果表单出现问题,您需要使用值再次显示它,您只需返回视图以及发送的模型
[HttpPost]
public ActionResult POHeader(FormModel model)
{
if (ModelState.IsValid)
{
// Do whatever you want to do and redirect to wherever you want to go from here.
}
// If the Model is invalid then simply return back the view along with the invalid model.
return View(model)
}
有关模型绑定的更多信息,请参阅下面的文章
下面是另一篇关于客户端模型验证的文章
// HTTP Get
public ActionResult POHeader(string sites, string user, string department,
string status, string PONumber, string TransactionDate, bool IsRedirectAction = false)
{
// Populate Dropdown List
GetSiteDropdownList(SiteId);
GetUserDropdownList(UserId);
GetDepartmentDropdownList(DepartmentId);
GetStatusDropdownList(StatusId);
var PO = from p in db.X_PO_HDR
select p;
// Get Selected Site
if ((!string.IsNullOrEmpty(sites)) || ((TempData["selectedsite"] != null)))
{
if (sites.Length > 0)
{
if (IsRedirectAction)
{
SiteId = (string)TempData["selectedsite"];
//sites = SiteId;
}
else
{
SiteId = sites;
TempData["selectedsite"] = SiteId;
}
// Get Selected Site
PO = PO.Where(p => p.Site_Id == SiteId);
}
}
if (!string.IsNullOrEmpty(user) || ((TempData["selectedUser"] != null)))
{
if (user.Length > 0)
{
if (IsRedirectAction)
{
UserId = (string)TempData["selectedUser"];
}
else
{
UserId = user;
TempData["selectedUser"] = UserId;
}
// Filter by User
PO = PO.Where(p => p.Created_By == UserId);
}
}
// Get Selected Department
if (!string.IsNullOrEmpty(department) || ((TempData["selectedDepartment"] != null)))
{
if (department.Length > 0)
{
if (IsRedirectAction)
{
DepartmentId = (string)TempData["selectedDepartment"];
}
else
{
DepartmentId = department;
TempData["selectedDepartment"] = DepartmentId;
}
// Filter by Department
PO = PO.Where(p => p.Purch_Dept == DepartmentId);
}
}
PO = PO.OrderBy(o => o.Txn_DT);
// check if TempData contains some error message and if yes add to the model state.
if (TempData["CustomError"] != null)
{
ModelState.AddModelError(string.Empty, TempData["CustomError"].ToString());
}
return View(PO.ToList());
}
[HttpPost, ActionName("POHeader")]
[MultiButton(MatchFormKey = "action", MatchFormValue = "Reject")]
public ActionResult Reject(int[] selectedList)
{
string var1 = collection["sites"];
UpdateListStatus(selectedList, "X", "Rejected");
TempData["CustomError"] = selectedList.Count().ToString() + " record(s) has been successfully " + ActionString + "!";
return RedirectToAction("POHeader", new { IsRedirectAction = true });
}
[HttpPost]
public ActionResult POHeader(FormModel model)
{
if (ModelState.IsValid)
{
// Do whatever you want to do and redirect to wherever you want to go from here.
}
// If the Model is invalid then simply return back the view along with the invalid model.
return View(model)
}