Http is stateless. your state won't stay between your requests (in webforms, it is posssibel thru viewstate, but in MVC there is no viewstate). You need to reload the collection(Genders) before sending the model back the view.
Looks like you are using your domain models in your view. I would personally create a simple viewmodel to handle this.
public class PersonSearchVM
{
public string SelectedGender { set;get;}
public List<SelectListItem> Genders { set;get;}
public List<Person> Results { set;get;}
public PersonSearchVM()
{
Genders=new List<SelectListItem>();
Results =new List<Person>();
}
}
And your action methods, create an object of your viewmodel, load the Genders collection and send the view model to the view which is strongly typed to our view model.
public ActionResult Search()
{
var vm=new PersonSearchVM { Genders=GetGenders()};
return View(vm);
}
[HttpPost]
public ActionResult Search(PersonSearchVM model)
{
string selectedVal=model.SelectedGender;
if(!String.IsNullOrEmpty(selectedVal)
{
model.Results=GetSearchResultsFromSomeWhere(selectedVal);
}
model.Genders=GetGenders(); //reload dropdown items
model.SelectedGender=selected;
return View(model);
}
private List<SelectListItem> GetGenders()
{
return new List<SelectListItem> {
new SelectListItem { Value="Male",Text="Male"},
new SelectListItem { Value="Female",Text="Female"},
};
}
In your Search view, Use Html.DropDownListFor
html helper method.
@model PeopleSearchVM
@using(Html.BeginForm())
{
@Html.DropDownListFor(s=>s.SelectedGender,Model.Genders,"Select")
<input type="submit" value="Filter" />
@foreach(var person in Model.Results)
{
<p>@person.FirstName</p>
}
}