【发布时间】:2014-02-27 03:17:13
【问题描述】:
我有搜索表单:站点、用户、状态和日期。搜索后,我将执行拒绝、重新提交或批准以调用控制器操作以更新数据库中的状态。我的代码如下:
/// 查看:
@Html.ValidationMessage("CustomError")
@Html.ValidationSummary(true)
@using (Html.BeginForm()) // Begin Form
{
<table>
<tr>
<td>Site:</td>
<td>@Html.DropDownList("sites", (IEnumerable<SelectListItem>)ViewBag.sites, "-- ALL --", new { style = "width: 300px;" })</td>
<td>Status:</td>
<td>@Html.DropDownList("status", (IEnumerable<SelectListItem>)ViewBag.status, "-- ALL --", new { style = "width: 150px;" })</td>
<td>PO No:</td>
<td>@Html.TextBox("PONumber", null, new { style = "width:150px" })</td>
</tr>
<tr>
<td>User: </td>
<td>@Html.DropDownList("user", (IEnumerable<SelectListItem>)ViewBag.user, "-- ALL --", new { style = "width: 300px;" })</td>
<td>Department:</td>
<td>
@Html.DropDownList("department", (IEnumerable<SelectListItem>)ViewBag.department, "-- ALL --", new { style = "width: 150px;" })
</td>
<td>Transaction Date:
</td>
<td>
@*<input type="text" id="TransactionDate" name="TransactionDate" style="width:210px" />*@
@*@Html.TextBox("TransactionDate", null, new { @class="datefield", style = "width:150px", @Value = DateTime.Now.ToShortDateString() })*@
@Html.TextBox("TransactionDate", null, new { @class="datefield", style = "width:150px" })
</td>
</tr>
</table>
<input type="submit" value="Search" />
// Here is the search result table
<input type="submit" value="Re-Submit" name="action" />
<input type="submit" value="Approve" name="action" />
<input type="submit" value="Reject" name="action" />
} // 结束表单
//// 控制器
// 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 操作
[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 Action 是否需要再次传递所有参数? (有更好的解决方法吗?) 2. CustomError 消息不起作用。 (下次搜索完成后出现消息)
谢谢, 四四
【问题讨论】:
标签: asp.net-mvc