【问题标题】:Return the search result after HTTPPOST in MVC返回MVC中HTTPPOST后的搜索结果
【发布时间】: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


    【解决方案1】:

    我将从将视图绑定到模型开始。通过这样做,您可以将整个模型传递回您的方法,然后如果表单存在问题并且您需要使用值再次显示它,您只需将视图连同您发送的模型一起返回。

    [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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多