0

我有一个带有搜索选项的视图来过滤结果:

@model GWeb.Models.FilterModel
@using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        <fieldset>
            <legend>Search criteria</legend>

                @(Html.Telerik().ComboBox()
                    .Name("UserName")                
                    .BindTo(new SelectList(ViewBag.workerList as System.Collections.IEnumerable, "Id", "Desciption"))
                .Value(Model.UserName))

                @(Html.Telerik().DatePicker()
                .Name("StartWork")
                .Value(Model.StartWork))

                @(Html.Telerik().DatePicker()
                .Name("EndWork")
                .Value(Model.EndWork))       

                <input type="submit" value="Filter" />

        </fieldset>   
    }
    @{Html.RenderPartial("EmployeeList", (IEnumerable<GWeb.Entities.Work>)ViewBag.employeeList);}

RenderPartial 是可以编辑的项目列表:

<td>
<a href="@Url.Action("Edit", "Admin", new { id = item.Id })">
    <img src="/Content/edit.png" alt="Edit" title="Edit" width="22" height="22" />
</a>

编辑视图是标准的脚手架生成视图。FilterModel 包含:

public class FilterModel
{
    public string UserName { get; set; }
    public DateTime? StartWork { get; set; }
    public DateTime? EndWork { get; set; }
    //...
}

问题:当我编辑其中一项并返回主视图时,搜索条件消失了。我怎样才能记住设置为的值FilterModel?这样在编辑或查看列表中的项目后,我可以返回到之前设置的相同过滤器选项?

非常感谢任何帮助!

4

1 回答 1

1

这是一个常见的场景。我通常使用 GET 而不是 POST(默认)提交搜索表单,并将 Request.UrlReferrer 存储在 HttpSession 中。然后将此会话值用于取消和重定向操作。

这是一个例子:

<form action="~/Search/Index" method="GET">
     ...
     <submit />
</form>

class SearchController : Controller
{
     public ActionResult Index(FilterModel model = null)
     {
         Session["SearchUrl"] = Request.UrlReferrer.ToString();
         var results = get page of results...
         return View(results);
     }

     [HttpPost]
     public ActionResult Edit(EditModel model)
     {
         //update the model...

         return Redirect(Session["SearchUrl"]);
     }
}
于 2012-09-13T14:36:13.067 回答