당신이 저를 도울 수 있기를 바랍니다. dropdownlist.There에 중복 문제가 있습니다. 선택 목록이므로 GroupBy를 사용하고 이런 식으로 피하십시오. 두 ActionResult Create 클래스는 동일한 두 가지를 가질 수 없습니다 parametres.And 그것은 작동하지 않습니다. 여기 내 컨트롤러 코드 : 드롭 다운 목록의 고유 항목
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 동작으로 어떻게 코드해야합니까? GET 동작에서 내 Viewbag를 어떻게 설정해야합니까?
질문이 명확하지 않습니다. 무엇이'Distinct'를 처음'Create' – techspider
에 적용하기가 어렵게 만들고 왜'ViewBag.Departure'에 2 가지 다른 데이터 세트를 가지고 있습니까? IT 부서는보기로 돌아 가기 전에 두 가지 작업 방법이 동일해야합니다. – Shyju
또한, 값이 아닌 유형 (예 : 복합 유형의 속성 인 경우 'EqualsTo'을 구현하려는 경우)에서 'Distinct'를 수행하는 것을 기억하십시오. 아직 수행하려는 작업을 이해하지 못합니다. ** 제발 DO를하십시오. 코멘트에 코드를 추가하지 마십시오.원래 질문에 ** ** – Shyju