Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.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# 如何从本地托管数据库填充下拉菜单?_C#_Asp.net_Sql Server_Database_Asp.net Mvc 4 - Fatal编程技术网

C# 如何从本地托管数据库填充下拉菜单?

C# 如何从本地托管数据库填充下拉菜单?,c#,asp.net,sql-server,database,asp.net-mvc-4,C#,Asp.net,Sql Server,Database,Asp.net Mvc 4,我现在真的被难住了。我已经被这个问题困扰了好几天了,坦白地说,我已经厌倦了 我有这个数据库表: (注意FlightsTable) 我的目标是根据“离开”部分的值填充下拉菜单 我已经尝试了很多东西,但我仍然无法掌握它 这是我的模型: public class FlightModel { public int FlightID { set; get; } public string Departure { set; get; } public string Arrival {

我现在真的被难住了。我已经被这个问题困扰了好几天了,坦白地说,我已经厌倦了

我有这个数据库表:

(注意
FlightsTable

我的目标是根据“离开”部分的值填充下拉菜单

我已经尝试了很多东西,但我仍然无法掌握它

这是我的模型:

public class FlightModel
{
    public int FlightID { set; get; }
    public string Departure { set; get; }
    public string Arrival { set; get; }
    public int NumberOfSeats { set; get; }
    public int NumberOfFlights { set; get; }

}
控制器:

    public ActionResult BookFlight()
    {
        return View();
    }
FlightDBEntities(来自FlightsDBModel)

namespace项目\u v3.Models
{
使用制度;
使用System.Data.Entity;
使用System.Data.Entity.Infrastructure;
公共部分类FlightsDBenties:DbContext
{
公共航班
:base(“name=flightsdbenties”)
{
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
抛出新代码FirstException();
}
公共DbSet FlightsTables{get;set;}
}
}
文件的屏幕截图:


如何在BookFlight视图中使用下拉菜单?我真的在努力解决这个问题,所以一步一步的解决方案,我可以理解,每一步都会非常感激。请并感谢您。

有几种方法可以做到这一点,以下是一个例子:

您将需要一些实际创建selectlist的东西:

public SelectList GetAsSelectList()
{
    var depts = from f in db.FlightsTables 
                        select new SelectListItem
                        {
                            Value = f.Departure,
                            Text = f.Departure
                        };
     return new SelectList(depts, "Value", "Text");
}
public SelectList DepartureList { get; set; }
如果离港记录在他们自己的表格中,那么最好从那里选择离港记录,使用
Id
主键字段作为selectlist的
字段

我假设selectlist将显示在与
FlightModel
相同的页面上。如果是这种情况,则您的模型需要selectlist的属性:

public SelectList GetAsSelectList()
{
    var depts = from f in db.FlightsTables 
                        select new SelectListItem
                        {
                            Value = f.Departure,
                            Text = f.Departure
                        };
     return new SelectList(depts, "Value", "Text");
}
public SelectList DepartureList { get; set; }
您的控制器方法需要创建一个FlightModel实例,用您想要显示的内容(包括selectlist)填充该实例,并将其传递给视图:

public ActionResult BookFlight()
{
   var model = new FlightModel
   {
       DepartureList = GetAsSelectList()
   };
   return View(model);
}
根据您希望用户对selectlist执行的操作,您可以在视图中显示它,如下所示:

@Html.DropDownList("Departure", Model.DepartureList)

在控制器中,选择不同的偏离

IEnumerable<string> departures = db.FlightsTables.Select(f => f.Departure).Distinct();
并且在视图中(不确定您要绑定到的属性-您尚未为
预订显示您的模型

我想您可能需要修改您的航班表。您有一个ID列,但有一个航班数列。您肯定需要关于每次航班起飞和到达时间的信息吗

编辑

进一步的评论,您的预订模式可能看起来像

public class BookingVM
{
  public List<FlightModel> FlightList { get; set; } // to display available flights
  public string Departure { get; set; } // bind to DepartureList
  public int FlightID { get; set; } // bind to FlightID in a dropdown that displays the arrival locations
  public int Seats { get; set; } // number of seats booked
  public SelectList DepartureList { get; set; } // unique departures
}
公共类预订虚拟机
{
public List FlightList{get;set;}//以显示可用航班
公共字符串出发{get;set;}//绑定到出发列表
public int FlightID{get;set;}//在显示到达位置的下拉列表中绑定到FlightID
公共int座位{get;set;}//预订的座位数
public SelectList出发点列表{get;set;}//唯一出发点
}

您需要处理离港下拉列表的
.change()
事件,使用ajax将所选离港传递给控制器方法,该方法返回flightID和到达位置(可能还有每个航班的可用座位数),并使用该方法填充第二个下拉列表(绑定到
FlightID
并显示到达位置)。

您好。谢谢您的回复!我真的非常感谢您的回复。因此,我应该将
SelectList GetAsSelectList()
放入
FlightModel.cs
?或者我正在看其他东西吗?
GetAsSelectList()
可以在模型类中,但最好将它放在控制器中。另一个问题是关于db.FlightsTables中的f的
var depts=from的问题。我收到一个错误,说db在当前上下文中不存在?这里是否缺少一行?(我在控制器中放置了
GetAsSelectList()
)这是因为它没有对您的数据类的引用。您如何访问项目中其他地方的数据库?请使用相同的方法。感谢编程之神!我现在让它工作起来了。您是一个绝对的明星!有没有办法让我细化下拉列表,使其仅显示一次
伦敦
d然后创建一个不同的dropbox,其中包含
到达
目的地?您好。非常感谢您的回复!我还没有预订,但是我的想法是将航班ID或起飞+到达分配给用户。目前,我正打算一步一步地这样做(即,为起飞和到达创建下拉列表,并确保它们都来自数据库。之后,我可以将这些下拉列表中的值分配给用户etcSee edit-我对您的航班表有一些疑问。我将再次更新有关预订模式的建议。快速问问题。
db
c在哪里oming from?在您发布的第一行代码中?再次更新了建议的预订模式。
db
是您的数据库上下文-查看您的问题,我假设它的
flightsdbenties
我现在就知道了。谢谢!另外,非常感谢您编辑如何设置我的预订模式。
public class BookingVM
{
  public List<FlightModel> FlightList { get; set; } // to display available flights
  public string Departure { get; set; } // bind to DepartureList
  public int FlightID { get; set; } // bind to FlightID in a dropdown that displays the arrival locations
  public int Seats { get; set; } // number of seats booked
  public SelectList DepartureList { get; set; } // unique departures
}