用于搜索的链接模式视图列表的MVC 3 Dropdownlist在回发时始终为null



我有一个带过滤器的搜索表单。1过滤器是一个包含数字的下拉列表。我有一个使用viewmode的视图,它有一个可数的搜索范围(我的数据库表),在控制器中我填充它,所有的工作都应该如此。。。直到表单显示出来,用户再次尝试过滤,在这种情况下,我的模型视图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>

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

private static IEnumerable SearchRange { get; set; }
static SearchController()
{
   SearchRange = new MyDataContext().SearchRanges.AsEnumerable();
}
public ActionResult Jobs(ViewModels.SearchResultsViewModel model)
{ 
    model.Range = SearchRange;
    ...
}

最新更新