Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7571121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:35:48+00:00 2026-05-30T15:35:48+00:00

I have a search form with filters. 1 filter is range which is to

  • 0

I have a search form with filters. 1 filter is range which is to be a dropdown with numbers. I have a view using a viewmodal with a ienumerable of search range (my db table) and in the controller i populate this and all works as it should… until the form is displayed and the user tries to filter again in which case my model view ienumerable is now null. Why does it lose the values. Do i need to re-popualte this everytime the users does a new filter… if so can it be done without repeating calls to the database as the values in the dropdown list wont change.

modal view

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; } 

}

controller

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);


    } 

View

@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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:35:49+00:00Added an answer on May 30, 2026 at 3:35 pm

    If the values in the dropdown list won’t change (ever) then you should probably be caching them server side and simply reusing the values each time. You will need to supply the values to the model each time as the data does not persist between successive requests. There is NO viewstate in MVC to retain the values. This is, actually, a good thing.

    private static IEnumerable SearchRange { get; set; }
    
    static SearchController()
    {
       SearchRange = new MyDataContext().SearchRanges.AsEnumerable();
    }
    
    public ActionResult Jobs(ViewModels.SearchResultsViewModel model)
    { 
        model.Range = SearchRange;
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have a search results page which returns queries form the database using
(Using MySQL and PHP) I have a search form that will allow my users
I'm making a simple search form in rails. In my search view I have
I have a class project where I am using a VB.Net form to search
We have a search form where the filter is bound to a property on
i have database table with few text fields which i use to filter data
I have a search form with min and max fields. It queries a MySQL
I have a simple search form with a box and a button. <form action
what I have: a google search form (displayed on all pages) a wordpress page
How do you add Search form to your Views in Drupal? Do I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.