在MVC中的HTTPPOST之后返回搜索结果

时间:2014-02-27 03:17:13

标签: asp.net-mvc

我有搜索表单来搜索:网站,用户,状态和日期。搜索完成后,我将执行拒绝,重新提交或批准以调用控制器操作以更新数据库中的状态。我的代码如下:

///查看:

@Html.ValidationMessage("CustomError")

@ Html.ValidationSummary(真)

@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 Action

[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消息不起作用。 (下次搜索完成后会显示消息)

谢谢, Si Thu

1 个答案:

答案 0 :(得分:0)

我首先将View绑定到模型。通过这样做,您可以将整个模型传递回您的方法,然后如果表单出现问题,您需要再次使用值显示它,您只需返回视图以及您发送的模型。

[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://www.codeproject.com/Articles/159749/ASP-NET-MVC-Model-Binding-Part1

这是另一篇关于客户端模型验证的文章 http://www.codeproject.com/Articles/718004/ASP-NET-MVC-Client-Side-Validation