Daniil Daniil - 3 months ago 31
ASP.NET (C#) Question

Unique items in dropdownlist

I hope you guys can help me,I have some troubles with dropdownlist.There are duplicates im my selectlist,so I use GroupBy and avoid them this way,but two ActionResult Create classes can't have two identical parametres.And it doesn't work.
Here is my controller code:

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);
}


As you see I want to have only unique items in my dropdownlist.

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


If I do this

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();
}


I have an error with identical parameter.

If this way which is obviously wrong.

public ActionResult Create()
{

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


An exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll but was not handled in user code

Additional information: There is no ViewData item of type 'IEnumerable' that has the key 'Departure'.

So how I should code it in GET action that it would work?How I should set my Viewbag in GET action?

Answer

Your razor view code is expecting a ViewBag with name Departure. So make sure you set that in your GET action.

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();
}

Assuming you have the below code in your view.

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

This should fix your error "There is no ViewData item of type 'IEnumerable' that has the key 'Departure'."

Assuming you want to show all the unique Departure column values from the Flight table. If you want a subset, use a Where clause in your LINQ expression as needed.

Comments