https://www.codeproject.com/Articles/702890/MVC-Entity-Framework-and-Many-to-Many-Relation Beispiel folgend, habe ich den nächsten Fehler, wenn meine Edit.cshtml Posting:ListBoxFor ‚List‘ Fehler zu müssen sein ein ‚SelectedListItem‘
„Der Viewdata Element, das die wichtigsten‚SelectedTelephoneCellulaires‘hat, ist vom Typ 'System.Collections.Generic.List' muss aber vom Typ 'IEnumerable' sein. "
Ich suchte online (natürlich!), Aber die beiden am engsten verwandten Probleme-Antworten (Issues converting types to new selectitemlist & & @Html.DropDownListFor not posting back to controller) brachte keine Lösung für meine Schwierigkeiten.
Hier ist ein Teil meiner Viewmodel (auf Diät, zur besseren Lesbarkeit halber):
public partial class EmployeVM
{
public Employe Employe { get; set; } //Employe
public IEnumerable<SelectListItem> AllTelephoneCellulaires { get; set; } //TelephoneCellulaire
private List<int> _selectedTelephoneCellulaires;
public List<int> SelectedTelephoneCellulaires
{
get
{
if (_selectedTelephoneCellulaires == null)
{
_selectedTelephoneCellulaires = Employe.TelephoneCellulaire1.Select(m => m.IdTelephoneCellulaire).ToList();
}
return _selectedTelephoneCellulaires;
}
set { _selectedTelephoneCellulaires = value; }
}
}
}
Ein Teil meiner EmployesController (auf Ernährung als auch):
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(EmployeVM employeView)
{
if (employeView == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
if (ModelState.IsValid)
{
var employeToUpdate = db.Employe
.Include(e => e.TelephoneCellulaire1)
[...]
.First(e => e.IdEmploye == employeView.Employe.IdEmploye);
if (TryUpdateModel(employeToUpdate, "Employe", new string[] { "NomEmploye", "PrenomEmploye", "IdTitre", "IdDepartement", "IdSuperviseur", "DateEmbauche", "DateDepart", "StatutEmploye", "IdEmployeur", "IdLocalisation", "Langue", "CarteAcces", "TelephoneCellulaire", "IdTelephoneBureau", "CarteAffaire", "EquipementInformatique", "AdresseCourriel", "GroupeSecurite", "AccesApplicatif", "CodeAlarme", "CleBatiment", "VehiculeCompagnie", "DateNaissance", "IsSuperviseur", "IsActif" }))
{
var newTelephoneCellulaires = db.TelephoneCellulaire.Where(m => employeView.SelectedTelephoneCellulaires.Contains(m.IdTelephoneCellulaire)).ToList();
[...]
var updatedTelephoneCellulaires = new HashSet<int>(employeView.SelectedTelephoneCellulaires);
[...]
foreach (TelephoneCellulaire telephone in db.TelephoneCellulaire)
{
if (!updatedTelephoneCellulaires.Contains(telephone.IdTelephoneCellulaire))
{
employeToUpdate.TelephoneCellulaire1.Remove(telephone);
}
else
{
employeToUpdate.TelephoneCellulaire1.Add(telephone);
}
}
[...]
db.Entry(employeToUpdate).State = EntityState.Modified;
db.SaveChanges();
}
return RedirectToAction("Index");
}
if (!ModelState.IsValid)
{
foreach (var obj in ModelState.Values)
{
foreach (var error in obj.Errors)
{
if (!string.IsNullOrEmpty(error.ErrorMessage))
System.Diagnostics.Debug.WriteLine("ERROR WHY = " + error.ErrorMessage);
}
}
}
return View(employeView);
}
Und schließlich, meine Edit.cshtml (Sie haben es geraten - auf Diät ...)
<div class="form-group">
@Html.LabelFor(model => model.AllTelephoneCellulaires, "Téléphones cellulaires", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.ListBoxFor(m => m.SelectedTelephoneCellulaires, Model.AllTelephoneCellulaires)
</div>
</div>
Beide adressierten Präzedenzfall Antworten die Tatsache, dass „Model.AllTelephoneCellulaires“ den Fehler verursachen würde, nicht aber das „m.SelectedTelephoneCellulaires“
Das Lustigste daran ist, dass ich war in der Lage, eine Weile her, einen Angestellten zu bearbeiten, ohne jedes Problem, aber wenn ich versuchte, ein anderes zu bearbeiten (beide ohne den 'TelephoneCellulaire' Eingang zu berühren), bang! Hier kam der Fehler.
Ich verstehe einfach nicht, warum ich diesen Fehler bekomme? Warum müsste meine SelectedThing vom Typ "SelectListItem" sein ?? Und wie kann ich es reparieren ??? Wenn mir jemand helfen kann, werde ich dir für immer verpflichtet sein. Gerade jetzt, ich bin versteckt unter meinem Schreibtisch weinen, Saugen meinen Daumen in Fötusposition ...
Danke, es hat funktioniert. Möge die Sonne jeden Tag auf dein Leben in Detroit oder anderswo scheinen ... :) –