从数据库中选择项目加载DropDownList并显示数据

时间:2015-05-06 20:11:29

标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

我有一个从数据库加载的下拉列表,我想根据下拉列表中的特定项目显示数据。在where子句中,它将下拉列表与所选项进行比较,并显示该事件的所有信息。

另外我对@ Html.DropDownList(" RaceEvent)有例外;

类型' System.InvalidOperationException'的例外情况发生在System.Web.Mvc.dll中但未在用户代码中处理

附加信息:具有键' RaceEvent'的ViewData项。属于' System.String'但必须属于' IEnumerable'。

控制器看起来像这样

private raceEntities db = new raceEntities();
    // GET: Registrants
    public ActionResult Index()
    {

        var regist = new SelectList(db.raceevents, "RaceEventID", "Name").ToString();

        ViewBag.RaceEvent = regist;

        /* iterate through raceevent table grab Name column convert to string
        var qry = (from rac in db.raceevents
                  select new { rac.Name }).ToString();
        */

        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"
                           where raceeve.Name == regist  //raceeve.Name matches the selectlist above 
                          // where raceeve.Name == qry //compare event name with the query 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 = "Event Registrants";
}

<h2>Event Registrants</h2>
@using (Html.BeginForm())
{
@Html.DropDownList("RaceEvent") //references from ViewBag.RaceEvent in the controller

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

}

2 个答案:

答案 0 :(得分:0)

*仅下拉列表修复。您可以在脚本元素中执行ajax调用,并填充传递所选SelectListItem的Id的div。

最好创建一个视图模型。

visible: isExpanded

控制器

public class RegistrantsViewModel
{
    public RegistrantsViewModel()
    {
        RaceEvents = new List<SelectListItem>();
    }

    public IEnumerable<IDFWebApp.Models.Custom.Registrants> Registrants { get; set; }

    public List<SelectListItem> RaceEvents { get; set; }

    public Int32 SelectedItem { get; set; }

}

查看

    public ActionResult Index()
    {
        //Create a new view model.
        RegistrantsViewModel oViewModel = new RegistrantsViewModel();

        //Pull back all events from the datasource.
        IEnumerable<RaceEvent> lAllRaceEvents = db.raceevents;

        //Iterate over every event.
        foreach (RaceEvent lEvent in lAllRaceEvents)
        {
            //Map each event to a select list item and attach it to the view model.
            oViewModel.RaceEvents.Add(new SelectListItem{ Text = lEvent.EventName, Value = lEvent.Id });
        }

        //iterate through raceevent table grab Name column convert to string
        //Also attach to the view model.
        oViewModel.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"
                                   where raceeve.Name == regist  //raceeve.Name matches the selectlist above 
                                   // where raceeve.Name == qry //compare event name with the query above
                                   select new Registrants { LastName = per.LastName, FirstName = per.FirstName, RoleType = organizationrolety.Name }).Distinct().OrderBy(per => per.LastName);

        return View(oViewModel);
    }

答案 1 :(得分:0)

您需要从.ToString();

中删除var regist = new SelectList(db.raceevents, "RaceEventID", "Name").ToString();

使用@Html.DropdownList()的{​​{1}},您指定表单字段的名称时,该值必须为IEnumerable<SelectListItem>SelectList为),但您正在转换它是一个值为"System.Web.MVC.SelectList"的字符串,因此需要

var regist = new SelectList(db.raceevents, "RaceEventID", "Name");