The goal was to change the items of a secondary drop down list when the item of the primary drop down list has changed.
With extremely confident and optimistic ideas, I came up with this simple code :
Model :
public class ChoixModel
{
public INSC_Inscription inscription { get; set; }
public List<Ecole> ecoles { get; set; }
public List<Programme> programmes { get; set; }
public int? ecoleChoi1 { get; set; }
public int? programmeChoi1 { get; set; }
}
Controler :
public ActionResult Phase1(int idInscrip)
{
inscriptionPrimaireEntities db = new inscriptionPrimaireEntities();
ChoixModel model = new ChoixModel();
if (model.ecoleChoi1 == null)
model.ecoleChoi1 = db.Ecole.FirstOrDefault()?.id;
model.ecoles = db.Ecole.ToList();
model.programmes = db.Programme.Where(t => t.FK_Ecole == model.ecoleChoi1).ToList();
return View(model);
}
[HttpPost]
public ActionResult Phase1(ChoixModel model, string submiter)
{
inscriptionPrimaireEntities db = new inscriptionPrimaireEntities();
if (!string.IsNullOrWhiteSpace(submiter))
{
if (model.ecoleChoi1 != null)
model.programmeChoi1 = db.Programme.FirstOrDefault(t => t.id == model.inscription.Programme1)?.id;
}
return View(model);
}
View :
@using System.Web.UI.WebControls
@using inscriptionPrimaire.Models
@model ChoixModel
@{
ViewBag.Title = "Phase1";
Layout = "~/Views/Shared/_Layout.cshtml";
SelectList selectEcole = new SelectList(Model.ecoles, "id", "NomAffiche");
SelectList selectPrograme = new SelectList(Model.programmes, "id","NomAffiche");
}
<form method="post" action="@Url.Action("Phase1", "Choix")" id="ViewModelForm">
<br /><br />
<table>
<tr>
<td style="min-width: 152px;">
@Html.DropDownListFor(t => t.ecoleChoi1, selectEcole, new { @class = "form-control", style = "min-width: 150px;", onchange = "this.form.submit();" })
</td>
<td style="min-width: 152px;">
@Html.DropDownListFor(t => t.programmeChoi1, selectPrograme, new { @class = "form-control", style = "min-width: 150px;" })
</td>
</tr>
</table>
</form>
The error occurs when I click to change the selected item of the primary drop down list Ecole
. I suspect it has something to do with how I use submit on this :
@Html.DropDownListFor(t => t.ecoleChoi1, selectEcole, new { @class = "form-control", style = "min-width: 150px;", onchange = "this.form.submit();" })
Intended behavior was that when I change the selected item of drop down Ecole
, then it would fill the drop down Programme
with the actual programs offered by the Ecole
selected.
Instead I have an error, an error that debug can't even catch, it shows only in the browser :
an item with the same key has already been added
So I went to look in my db if there was ANY duplicate... There is not even a single line in both tables that contains ANYTHING identical to the previous one.
I've been asked to show full stack trace (it's in french) :
Erreur du serveur dans l'application '/'.
Un élément avec la même clé a déjà été ajouté.
Description : Une exception non gérée s'est produite au moment de l'exécution de la requête Web actuelle. Contrôlez la trace de la pile pour plus d'informations sur l'erreur et son origine dans le code.
Détails de l'exception: System.ArgumentException: Un élément avec la même clé a déjà été ajouté.
Erreur source:
Une exception non gérée s'est produite lors de l'exécution de la requête Web actuelle. Les informations relatives à l'origine et l'emplacement de l'exception peuvent être identifiées en utilisant la trace de la pile d'exception ci-dessous.
Trace de la pile:
[ArgumentException: Un élément avec la même clé a déjà été ajouté.]
System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) +52
System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +12534430
System.Collections.Generic.CollectionExtensions.ToDictionaryFast(TValue[] array, Func`2 keySelector, IEqualityComparer`1 comparer) +116
System.Web.Mvc.ModelBindingContext.get_PropertyMetadata() +136
System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) +176
System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) +101
System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +55
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +1197
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +330
System.Web.Mvc.DefaultModelBinder.GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder) +17
System.Web.Mvc.DefaultModelBinder.BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor) +377
System.Web.Mvc.DefaultModelBinder.BindProperties(ControllerContext controllerContext, ModelBindingContext bindingContext) +101
System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +55
System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +1197
System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +330
System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +331
System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +105
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__19(AsyncCallback asyncCallback, Object asyncState) +743
System.Web.Mvc.Async.WrappedAsyncResult`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +14
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +343
System.Web.Mvc.Controller.<BeginExecuteCore>b__1c(AsyncCallback asyncCallback, Object asyncState, ExecuteCoreState innerState) +25
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +465
System.Web.Mvc.Controller.<BeginExecute>b__14(AsyncCallback asyncCallback, Object callbackState, Controller controller) +18
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +20
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +374
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +16
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +52
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30
System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +384
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155