如何从过滤器/选择框中搜索数据库?

时间:2014-05-01 16:15:31

标签: c# asp.net-mvc razor

我有一个页面,其中有一个搜索框,我可以搜索sql数据库并在表格中显示结果。我现在有多个ListBoxFor()选择我想要设置为选项过滤器的框。 ListBoxFor正确地从数据库中填充。

我的问题是:

如何从过滤器/选择框中搜索数据库?

型号:

 public int[] SelectedIds { get; set; }
 public List<LookupAgency> Agencies { get; set; } //many more like this below...

控制器:

        [HttpPost]
        public ViewResult Index(string searchString, HomePageModel hpmodel)
        {
             // all projects
            var projects = _projectRepo.All;    
            var selectedAgencies = hpmodel.SelectedIds;

            var selectedAgencyItems = new List<LookupAgency>();
            int[] selectedAgencyArray = selectedAgencyItems.Select(s => s.Id).ToArray();

            // if we have a search string, search on it and filter results
            if (!String.IsNullOrEmpty(searchString))
            {
                projects = _projectRepo.All;
                projects = projects.Where(x => x.Description.Contains(searchString) ||
                     x.TIPNumber.Contains(searchString.ToLower()) ||
                     x.ProjectName.Contains(searchString)).OrderBy(x => x.ProjectName);
            }

            // return the projects to the view
            HomePageModel hpm = new HomePageModel();
            hpm.Project = projects;
            hpm.SelectedIds = selectedAgencies;
            hpm.Agencies = _projectRepo.GetAgencyItems();
            hpm.Categories = _projectRepo.GetCategoriesItems();
            hpm.ProjectTypes = _projectRepo.GetProjectTypes();
            hpm.FiscalYear = _projectRepo.GetFiscalYear();
            hpm.Phases = _projectRepo.GetPhases();
            hpm.FundingSources = _projectRepo.GetFundingSource();
            hpm.FundingTypes = _projectRepo.GetFundingTypes();
            hpm.MapService = _mapserviceRepo.All;

            return View(hpm);
        }

观点:

 @using (Html.BeginForm())
{
    <p>
        Search Database: @Html.TextBox("SearchString")    

        <!--Search Filter Multiselect-->
        <h5>Project Search</h5>
        <div>
            <h6>Search by Agency</h6>
            @Html.ListBoxFor(x => x.SelectedIds, new SelectList(Model.Agencies, "Id", "Name"))
        </div>
        <div>
            <h6>Search by Project Category</h6>
            @Html.ListBoxFor(x => x.SelectedIds, new SelectList(Model.Categories, "Id", "Name"), new { id = "projCategoryListBox" })
        </div>
        <div>
            <h6>Search by Project Type</h6>
            @Html.ListBoxFor(x => x.SelectedIds, new SelectList(Model.ProjectTypes, "Id", "Description"))
        </div>
        <h5>Funding Search</h5>
        <div>
            <h6>Search by Fiscal Year</h6>
            @Html.ListBoxFor(x => x.SelectedIds, new SelectList(Model.FiscalYear))
        </div>
        <div>
            <h6>Search by Phase</h6>
            @Html.ListBoxFor(x => x.SelectedIds, new SelectList(Model.Phases, "Id", "Name"))
        </div>
        <div>
            <h6>Search by Funding Source</h6>
            @Html.ListBoxFor(x => x.SelectedIds, new SelectList(Model.FundingSources, "Id", "Name"))
        </div>
        <div>
            <h6>Search by Funding Type</h6>
            @Html.ListBoxFor(x => x.SelectedIds, new SelectList(Model.FundingTypes, "Id", "Name"))
        </div>     
        <input type="submit" value="Find" autocomplete="on" />
    </p>
}

0 个答案:

没有答案