下拉列表中的唯一项

本文关键字:唯一 下拉列表 | 更新日期: 2023-09-27 18:02:33

我希望你们能帮助我,我有一些问题下拉列表。在我的选择列表中有重复的,所以我使用GroupBy并避免这种方式,但是两个ActionResult Create类不能有两个相同的参数。但这行不通。这是我的控制器代码:

public ActionResult Create(Flight flight)
{
        var departures = db.Flights.Where(m => m.Departure == flight.Departure)
         .OrderBy(m => m.Departure)
         .Select(i => i.Departure)
         .Distinct();
    ViewBag.Departure = new SelectList(departures);
    ViewBag.CompanyId = new SelectList(db.Companies, "CompanyId", "Name");
    ViewBag.TransportId = new SelectList(db.Transport, "TransportId", "Name");
    return View();
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,FlightId,Departure,Destination,Day,DepartureTime,ArrivalTime,Seats,Cost,TransportId,CompanyId")] Flight flight)
{
    try
    {
    if (ModelState.IsValid)
    {
        db.Flights.Add(flight);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    }
    catch (DataException /* dex */)
    {
        //Log the error (uncomment dex variable name and add a line here to write a log.
        ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
    }
    var departures = db.Flights.Where(m => m.Departure == flight.Departure)
         .OrderBy(m => m.Departure)
         .Select(i => i.Departure)
         .Distinct();
    ViewBag.Departure = new SelectList(departures);
       ViewBag.CompanyId = new SelectList(db.Companies, "CompanyId", "Name", flight.CompanyId);
    ViewBag.TransportId = new SelectList(db.Transport, "TransportId", "Name", flight.TransportId);
    return View(flight);
}

正如你所看到的,我希望下拉列表中只有唯一的项。

@Html.DropDownList("Departure", null, htmlAttributes: new { @class = "form-control" })

如果我这样做

public ActionResult Create(Flight flight)
{
        var departures = db.Flights.Where(m => m.Departure == flight.Departure)
         .OrderBy(m => m.Departure)
         .Select(i => i.Departure)
         .Distinct();
    ViewBag.Departure = new SelectList(departures);
    ViewBag.CompanyId = new SelectList(db.Companies, "CompanyId", "Name");
    ViewBag.TransportId = new SelectList(db.Transport, "TransportId", "Name");
    return View();
}

我有一个相同的参数错误。

如果这样做显然是错误的。

public ActionResult Create()
    {
        ViewBag.CompanyId = new SelectList(db.Companies, "CompanyId", "Name");
        ViewBag.TransportId = new SelectList(db.Transport, "TransportId", "Name");
        return View();
    }

类型为'System '的异常。InvalidOperationException'在System.Web.Mvc.dll中发生,但未在用户代码中处理

附加信息:没有'IEnumerable'类型的ViewData项有'Departure'键。

那么我应该如何在GET动作中编码它才能工作呢?我应该如何设置我的Viewbag在GET行动?

下拉列表中的唯一项

你的razor视图代码期望一个名为Departure的ViewBag。所以一定要在GET操作中设置好。

public ActionResult Create()
{
    var departures = db.Flights.OrderBy(m => m.Departure)
                                                     .Select(i => i.Departure).Distinct();
    ViewBag.Departure = new SelectList(departures);
    ViewBag.CompanyId = new SelectList(db.Companies, "CompanyId", "Name");
    ViewBag.TransportId = new SelectList(db.Transport, "TransportId", "Name");
    return View();
}

假设您的视图中有以下代码:

@Html.DropDownList("Departure", null, htmlAttributes: new { @class = "form-control" })

这应该修复你的错误"没有'IEnumerable'类型的ViewData项具有键'Departure' "。

假设您想显示Flight表中所有唯一的Departure列值。如果需要一个子集,可以根据需要在LINQ表达式中使用Where子句。

你在这里做错了几件事。您的第一个Create (GET)不应该占用您的航班。它根本没有被使用。如果你必须有航班对象才能知道哪些班次是可用的,只需输入航班ID,这样你就可以在数据库中找到它。

对于列表,创建一个新类来构建选择列表。然后从你的角度来看,你可以调用它

public static class SelectListsHelper
{
    public static SelectListsHelper Instance { get; } = new SelectListsHelper();
    public static List<SelectListItem> GetDepartures(long flightId)
    {
        var list = new List<SelectListItem>()
        //Build up your list here.
        return list;
    }
}

从视图调用,如下所示:

@Html.DropDownListFor(model => model.Departure, SelectListHelper.Instance.GetDepartures(Model.Id))