根据下拉列表选择从数据库中抓取数据

时间:2015-05-05 00:36:53

标签: c# asp.net asp.net-mvc asp.net-mvc-4

我正在将Windows桌面应用程序移植到ASP.NET MVC应用程序。目前我在数据库中有一个存储一系列竞赛事件的表,我想显示该事件的注册人列表。我通过硬编码数据库中的特定事件标题设法显示所有注册人。我已设法从数据库表中加载下拉列表。现在我想根据下拉列表选择的项目显示注册者,但我真的不知道如何将选择列表转换为字符串,因此where子句可以匹配内容。

这是控制器

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

这是视图

@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>

}

1 个答案:

答案 0 :(得分:1)

你有一些问题。首先,您缺少的是如何构建视图使用的模型。您需要一些东西来保存用户传递回控制器的数据。我添加了RaceEvent属性。您还需要一个属性来保存查询结果。那是Registrants属性。我添加了重命名的RegistrantsModel,但与您现在使用的模型类似。

namespace IDFWebApp.Models.Custom
{
    public class RegistrantsModel
    {
        public string LastName { get; set; }
        public string FirstName { get; set; }
        public organizationrolety RoleType { get; set; }
    }

    public class RegistrantsIndexModel
    {
        public string RaceEvent { get; set; }
        public IEnumerable<IDFWebApp.Models.Custom.RegistrantsModel> Registrants { get; set; }
    }
}

您还需要控制器中的post方法:

// Post: Registrants
[HttpPost]
public ActionResult Index(IDFWebApp.Models.Custom.RegistrantsModel model)
{
    model.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 == model.RaceEvents
            select new Registrants { LastName = per.LastName, FirstName = per.FirstName, RoleType = organizationrolety.Name }).Distinct().OrderBy( per => per.LastName);

    return View(model);
}

这可能不是一切,但它应该让你更远。