简体   繁体   English

ASP.NET组合框MVC

[英]Combo box asp.net mvc

I have datatable in Index.cshtml scaffold-ed from Bus.cs 我在Bus.cs的Index.cshtml支架中有数据表

    public class Bus
    {
        public int BusID { get; set; }

        public int BusOwnerID { get; set; }

        public string RegistrationNo { get; set; }
    }

In the index page i need to include a dropdownlist with BusOwnerID and BusOwnerName from BusOwner.cs 在索引页面中,我需要包含一个包含BusOwner.cs中的BusOwnerID和BusOwnerName的下拉列表

    public class BusOwner
    {
        public int BusOwnerID { get; set; }

        public string BusOwnerName { get; set; }

        public string Moblie { get; set; }

        public string EmailID { get; set; }
    }

In the controller i have the following, 在控制器中,我有以下内容,

    public ActionResult Index()
    {
        ViewBag.Genres = db.BusOwners.Select(i => new SelectListItem { Value = i.BusOwnerID.ToString(), Text = i.BusOwnerName });

        return View(db.Buses.ToList());
    }

In the view, index.cshtml i need a dropdownbox with BusOwnerID and BusOwnerName. 在视图中,index.cshtml我需要一个具有BusOwnerID和BusOwnerName的下拉框。 On selecting i need to display details of buses which comes under selected BusOwnerID. 选择后,我需要显示所选BusOwnerID下的总线详细信息。

I tried the following 我尝试了以下

    @Html.DropDownListFor(model => model.BusOwnerID, new SelectList(ViewBag.Genres), "Choose... ")

But error occurs. 但是会发生错误。

        CS1061: 'System.Collections.Generic.IEnumerable<RaspberryPi.Models.Bus>' does not contain a definition for 'BusOwnerID' and no extension method 'BusOwnerID' accepting a first argument of type 'System.Collections.Generic.IEnumerable<RaspberryPi.Models.Bus>' could be found (are you missing a using directive or an assembly reference?)

Index.cshtml Index.cshtml

    @model IEnumerable<RaspberryPi.Models.Bus>

    @{
        ViewBag.Title = "Buses";
    }

    <div class="btn-group">
        <a href="/Buses/Create" class="btn btn-primary btn-lg">Create New</a>

    </div>

    @*@Html.LabelFor(model => model.BusOwnerID)
    @Html.DropDownListFor(model => model.SelectRegionId, Model.Regions, "Choose... ")


    @Html.DropDownList("drop", new SelectList(ViewBag.Genres));*@

    <div id="Ttable" style="background:#f5f5f5;position:relative;width:100%;height:25%;">
        <table id="example" class="display" cellspacing="0" width="100%">
            <thead>
                <tr>
                    <th>@Html.DisplayNameFor(model => model.RegistrationNo)</th>
                    <th>@Html.DisplayNameFor(model => model.BusTypes)</th>
                    <th>Edit</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td>
                            @Html.DisplayNameFor(model => model.RegistrationNo)
                        </td>
                        <td>
                            @Html.DisplayNameFor(model => model.BusTypes)
                        </td>
                        <td>
                            <div class="btn-group">
                                <button type="button" class="btn-primary btn-xs" onclick="location.href = '/Buses/Details/@item.BusID';"><span class="glyphicon glyphicon-th"></span></button>
                                <button type="button" class="btn-success btn-xs" onclick="location.href = '/Buses/Edit/@item.BusID';"><span class="glyphicon glyphicon-pencil"></span></button>
                                <button type="button" class="btn-danger btn-xs" onclick="location.href = '/Buses/Delete/@item.BusID';"><span class="glyphicon glyphicon-remove"></span></button>
                            </div>
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>

How can i achieve that? 我该如何实现?

Please help, Thanks. 请帮忙,谢谢。

Do it this way 这样做

Make list of BusOwner on your view before creating Dropdown 在创建下拉列表之前,在您的视图上列出BusOwner的列表

var busOwnerList=BusOwner.GetAll(); //Make static method in your class to return all owner

now 现在

@Html.DropDownListFor(model => model.BusOwnerID, new SelectList(busOwnerList, "BusOwnerID", "BusOwnerName") as SelectList, "Choose... ")

The error indicates that there is no Property BusOwnerId present in your model. 该错误表明模型中不存在Property BusOwnerId Also the error tells us that the model is of type IEnumerable . 错误还告诉我们该模型的类型为IEnumerable

In your case BusOwnerId is only present on a single instance of a Bus and not on a collection of busses. 在您的情况下, BusOwnerId在于单个Bus实例上,而不存在于一组总线上。 So there are two options. 因此,有两种选择。

1 Return a single bus to the view as the model 1将单个总线作为模型返回视图

instead of: 代替:

return View(db.Buses.ToList()); 返回View(db.Buses.ToList());

Try 尝试

return View(db.Buses.FirstOrDefault());

Which will return the first bus from the database. 它将从数据库返回第一条总线。

2 Or you could loop through each bus entry in the model and display the dropdownbox for each bus: 2或者,您可以遍历模型中的每个总线条目,并显示每个总线的下拉框:

foreach(var bus in Model)
{
     @Html.DropDownListFor(x => bus.BusOwnerID, new SelectList(ViewBag.Genres), "Choose... 
}

note that in the lamda i bind to x => bus.busOwnerId instead of x => x.BusOwnerId because x.BusOwnerId indicates that BusOwnerId is a property on the model. 请注意,在lamda中,我绑定到x => bus.busOwnerId而不是x => x.BusOwnerId因为x.BusOwnerId指示BusOwnerId是模型上的属性。 Which isn't because the model is a collection. 这不是因为模型是集合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM