我希望你们可以帮助我,我在dropdownlist中遇到了一些麻烦。我的选择列表中有重复项,所以我使用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.Web.Mvc.dll中发生了'System.InvalidOperationException'类型的异常,但未在用户代码中处理
附加信息:没有“IEnumerable”类型的ViewData项目具有“离开”键。
那么我应该如何在GET操作中对它进行编码呢?我应该如何在GET操作中设置我的Viewbag?
答案 0 :(得分:0)
你这里做错了几件事。您的第一次创建(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))
答案 1 :(得分:0)
您的剃刀视图代码需要名为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项目具有”离开“ 键。”
假设您要显示Flight表中的所有唯一Departure
列值。如果需要子集,请根据需要在LINQ表达式中使用Where子句。