Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 组合框asp.net mvc_C#_Asp.net_Asp.net Mvc_Drop Down Menu_Asp.net Mvc 5 - Fatal编程技术网

C# 组合框asp.net mvc

C# 组合框asp.net mvc,c#,asp.net,asp.net-mvc,drop-down-menu,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Drop Down Menu,Asp.net Mvc 5,我在Index.cshtml中有一个数据表,它是从Bus.cs生成的 public class Bus { public int BusID { get; set; } public int BusOwnerID { get; set; } public string RegistrationNo { get; set; } } 在索引页面中,我需要包含一个带有BusOwnerID和BusOwnerName的下拉列表,

我在Index.cshtml中有一个数据表,它是从Bus.cs生成的

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

        public int BusOwnerID { get; set; }

        public string RegistrationNo { get; set; }
    }
在索引页面中,我需要包含一个带有BusOwnerID和BusOwnerName的下拉列表,该列表来自BusOwner.cs

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

        public string BusOwnerName { get; set; }

        public string Moblie { get; set; }

        public string EmailID { get; set; }
    }
在控制器中,我有以下内容:

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

        return View(db.Buses.ToList());
    }
在index.cshtml视图中,我需要一个带有BusOwnerID和BusOwnerName的下拉框。在选择时,我需要显示位于所选BusOwnerID下的总线的详细信息

我尝试了以下方法

    @Html.DropDownListFor(model => model.BusOwnerID, new SelectList(ViewBag.Genres), "Choose... ")
但是错误发生了

        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?)
CS1061:'System.Collections.Generic.IEnumerable'不包含'BusOwnerID'的定义,并且找不到接受'System.Collections.Generic.IEnumerable'类型的第一个参数的扩展方法'BusOwnerID'(是否缺少using指令或程序集引用?)
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>
@model IEnumerable
@{
ViewBag.Title=“总线”;
}
@*@LabelFor(model=>model.BusOwnerID)
@DropDownListFor(model=>model.SelectRegionId,model.Regions,“选择…”)
@DropDownList(“drop”,新的选择列表(ViewBag.Genres))*@
@DisplayNameFor(model=>model.RegistrationNo)
@DisplayNameFor(model=>model.BusTypes)
编辑
@foreach(模型中的var项目)
{
@DisplayNameFor(model=>model.RegistrationNo)
@DisplayNameFor(model=>model.BusTypes)
}
我怎样才能做到这一点

请帮忙, 谢谢。

这样做吧

在创建下拉列表之前,在视图中列出总线所有者

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


该错误表示您的模型中不存在属性
BusOwnerId
。 此外,错误告诉我们模型的类型为
IEnumerable

在您的情况下,
BusOwnerId
仅存在于
总线的单个实例上,而不存在于总线集合上。因此有两种选择

1将单个总线返回到视图作为模型

而不是:

返回视图(db.bus.ToList())

试一试

它将从数据库返回第一条总线

2或者,您可以循环浏览模型中的每个总线条目,并显示每个总线的下拉框:

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

请注意,在lamda中,我绑定到
x=>bus.busOwnerId
而不是
x=>x.busOwnerId
,因为
x.busOwnerId
表示busOwnerId是模型上的一个属性。这不是因为模型是一个集合。

错误是什么?你也能提供吗?@middelpat我把这个问题编辑错了
return View(db.Buses.FirstOrDefault());
foreach(var bus in Model)
{
     @Html.DropDownListFor(x => bus.BusOwnerID, new SelectList(ViewBag.Genres), "Choose... 
}