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

时间:2022-10-19 04:05:11

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.

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

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>

}

1 个解决方案

#1


You have a few issues. The first is you are missing is how to construct the model your view uses. You need something to hold the data the user is passing back to the controller. I added the RaceEvent property. You also need a property to hold the results of the query. That is the Registrants property. I added the RegistrantsModel which is renamed, but similar to the model you are using now.

你有一些问题。首先,您缺少的是如何构建视图使用的模型。您需要一些东西来保存用户传递回控制器的数据。我添加了RaceEvent属性。您还需要一个属性来保存查询结果。这是注册人的财产。我添加了重命名的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; }
    }
}

You also need a post method in your controller:

您还需要控制器中的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);
}

That probably isn't everything, but it should get you farther.

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

#1


You have a few issues. The first is you are missing is how to construct the model your view uses. You need something to hold the data the user is passing back to the controller. I added the RaceEvent property. You also need a property to hold the results of the query. That is the Registrants property. I added the RegistrantsModel which is renamed, but similar to the model you are using now.

你有一些问题。首先,您缺少的是如何构建视图使用的模型。您需要一些东西来保存用户传递回控制器的数据。我添加了RaceEvent属性。您还需要一个属性来保存查询结果。这是注册人的财产。我添加了重命名的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; }
    }
}

You also need a post method in your controller:

您还需要控制器中的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);
}

That probably isn't everything, but it should get you farther.

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