用于搜索的链接模态视图列表的MVC 3下拉列表在回发时始终为空

时间:2012-02-27 21:12:59

标签: asp.net-mvc asp.net-mvc-3 search

我有一个带过滤器的搜索表单。 1个过滤器是一个范围,它是一个带数字的下拉列表。我有一个视图使用一个具有可相关搜索范围(我的数据库表)的视图模式,并在控制器中我填充这个并且所有工作都应该......直到显示表单并且用户再次尝试过滤,在这种情况下我的模型视图ienumerable现在为null。为什么它会失去价值。每次用户使用新的过滤器时,我是否需要重新设置这个过滤器...如果是这样,可以在不重复调用数据库的情况下完成,因为下拉列表中的值不会改变。

模态视图

public class SearchResultsViewModel
{
    public int? Page { get; set; }
    public penguin.Models.Job Job { get; set; }
    public IPagedList<Models.Job> SearchResults { get; set; }
    public string SearchButton { get; set; }

    [Display(Name = "keywords")]
    public string keywords { get; set; }
    [Display(Name = "location")]
    public string location { get; set; }
    public int RangeID { get; set; }
    public IEnumerable<Models.SearchRange> Range { get; set; } 

}

控制器

const int RecordsPerPage = 25;
    public ActionResult Jobs(ViewModels.SearchResultsViewModel model)
    {

        // do something with the search value                    
        if (!string.IsNullOrEmpty(model.SearchButton) || model.Page.HasValue)
        {

            var entities = new Models.DBDataContext();
            var results = entities.Jobs;

            var pageIndex = model.Page ?? 1;
            model.SearchResults = results.ToPagedList(pageIndex, 25);

            //Range = ViewData["Range"] as IEnumerable<Models.SearchRange>; 
        }
        else {
            var Data = Helpers.ModelHelpers.GetDataContext();
            var ranges = (from r in Data.SearchRanges select r);
            model.Range = ranges;
            //Range = ranges;

        }
        //ViewData["Range"] = Range;
        //model.Range = Range;
        return View(model);


    } 

查看

@using PagedList.Mvc;
@model ViewModels.SearchResultsViewModel           
@{
    ViewBag.Title = "Jobs";
}
@section search {
 @using (Html.BeginForm("Jobs", "Search", FormMethod.Post, new { id = "searchForm" }))
{ 
    <fieldset>
        @Html.LabelFor(m => m.keywords)
        @Html.TextBoxFor(m => m.keywords)
        @Html.LabelFor(m => m.location)
        @Html.TextBoxFor(m => m.location)

        @Html.DropDownListFor(m => m.RangeID, new SelectList(Model.Range, "ID", "Range", 5))
        <input name="SearchButton" type="submit" value="Search" />
    </fieldset>

  }
 }
<h2>
Jobs</h2>
<div id="resultContainer">
my paged results set will go in here with a link to view job details. I then want
it to come back here and not lose anything 
@* html.Action("Results", "Search")*@
@if (Model.SearchResults != null && Model.SearchResults.Count > 0)
{
    foreach (var result in Model.SearchResults)
    {
    <hr />
    <table width="100%">
        <tr>
            <td valign="top">
                <div style="font-weight: bold; font-size: large;">
                    @Html.Raw(@result.Title.Truncate(50))</div>
                @Html.Raw(@result.summary.Truncate(50))<br />
                </td>
                    </tr>
    </table>

    }

    <hr />        

    @Html.PagedListPager(Model.SearchResults, page => Url.Action("Jobs", new RouteValueDictionary() {
                { "Page", page }, 
                { "Job", Model.Job }
                }), PagedListRenderOptions.DefaultPlusFirstAndLast)}
    </div>

1 个答案:

答案 0 :(得分:0)

如果下拉列表中的值不会改变(永远),那么您应该将它们缓存在服务器端,并且每次只重复使用这些值。每次因为数据在连续请求之间不存在时,您将需要向模型提供值。 MVC中没有视图状态来保留值。实际上,这是一件好事。

private static IEnumerable SearchRange { get; set; }

static SearchController()
{
   SearchRange = new MyDataContext().SearchRanges.AsEnumerable();
}

public ActionResult Jobs(ViewModels.SearchResultsViewModel model)
{ 
    model.Range = SearchRange;
    ...
}