I am porting a windows desktop application to an ASP.NET MVC application. Currently I have a table in the database that stores a bunch of race events and I want to display the list of registrants for that event. I have managed to display all the registrants by hard coding the specific event title from the database. I have managed to load the dropdownlist from the database table. Now I want to display the registrants based on a dropdownlist selected item, but I don't really know how to convert the selectlist as string so the where clause can match the content.
this is the controller
private raceEntities db = new raceEntities();
// GET: Registrants
public ActionResult Index()
{
ViewBag.RaceEvents = new SelectList(db.raceevents, "RaceEventID", "Name");
var registrants = (from per in db.people
join personorgrole in db.personorganizationroles on per.PersonID equals personorgrole.PersonID
join personeve in db.personevents on personorgrole.PersonOrganizationRoleID equals personeve.PersonOrganizationRoleID
join eventcla in db.eventclasses on personeve.RaceEventID equals eventcla.RaceEventID
join personeventcla in db.personeventclasses on eventcla.EventClassID equals personeventcla.EventClassID
join raceeve in db.raceevents on eventcla.RaceEventID equals raceeve.RaceEventID
join organizationrolety in db.organizationroletypes on personorgrole.OrganizationRoleTypeID equals organizationrolety.OrganizationRoleTypeID
//where raceeve.Name == "Ruapehu Gravity Festival" //hardcoded event title
//where raceeve.Name == RaceEvents raceeve.Name matches the selectlist above
select new Registrants { LastName = per.LastName, FirstName = per.FirstName, RoleType = organizationrolety.Name }).Distinct().OrderBy( per => per.LastName);
return View(registrants);
}
this is the view
@model IEnumerable<IDFWebApp.Models.Custom.Registrants>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
@Html.DropDownList("RaceEvents")
<table style="width:100%">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Role Type</th>
</tr>
@foreach (var person in Model)
{
<tr>
<td>@person.LastName</td>
<td>@person.FirstName</td>
<td>@person.RoleType</td>
</tr>
}
</table>
}
.change()event of the dropdownlist with javascript/jquery, then post the selected value to a controller method that returns a partial view (or json) of the filtered list and update the DOM. Although if you are initially displaying all items in the collection you could just filter the list on the client by showing/hiding the appropriate items