My problem is when I want to edit a row in my datatable, the fields with dropdown option show the defaut value instead of the value of the element selected but it shows the correct value for the fields without dropdown. This is my Edit Method:
[HttpGet]
public ActionResult AddorEdit(int id = 0)
{
if (id == 0)
{
ViewBag.Id_analyse = new SelectList(db.Analyse, "Id_analyse", "Id_analyse");
ViewBag.Id_client = new SelectList(db.Clients, "Id_client", "Nom_client");
ViewBag.Id_MT = new SelectList(db.Moyens_transport, "Id_MT", "Moyen");
ViewBag.Id_prestation = new SelectList(db.Prestations, "Id_prestation", "N_prestaContrôle");
ViewBag.Id_prod = new SelectList(db.Produit, "Id_prod", "Nom_produit");
ViewBag.Id_SA = new SelectList(db.Societé_d_acconage, "Id_SA", "Nom_SA");
ViewBag.Id_ST = new SelectList(db.Societé_de_transport, "Id_ST", "Nom_ST");
ViewBag.Id_cont = new SelectList(db.Societés_de_contrôle, "Id_cont", "Nom_scont");
ViewBag.Id_TK = new SelectList(db.TK, "Id_TK", "Nom_TK");
return View(new Export());
}
else
{
ViewBag.Id_analyse = new SelectList(db.Analyse, "Id_analyse", "Id_analyse");
ViewBag.Id_client = new SelectList(db.Clients, "Id_client", "Nom_client");
ViewBag.Id_MT = new SelectList(db.Moyens_transport, "Id_MT", "Moyen");
ViewBag.Id_prestation = new SelectList(db.Prestations, "Id_prestation", "N_prestaContrôle");
ViewBag.Id_prod = new SelectList(db.Produit, "Id_prod", "Nom_produit");
ViewBag.Id_SA = new SelectList(db.Societé_d_acconage, "Id_SA", "Nom_SA");
ViewBag.Id_ST = new SelectList(db.Societé_de_transport, "Id_ST", "Nom_ST");
ViewBag.Id_cont = new SelectList(db.Societés_de_contrôle, "Id_cont", "Nom_scont");
ViewBag.Id_TK = new SelectList(db.TK, "Id_TK", "Nom_TK");
return View(db.Export.Where(x => x.Id_export == id).FirstOrDefault<Export>());
}
}
[HttpPost]
public ActionResult AddorEdit(Export c)
{
if (c.Id_export == 0)
{
ViewBag.Id_analyse = new SelectList(db.Analyse, "Id_analyse", "Id_analyse");
ViewBag.Id_client = new SelectList(db.Clients, "Id_client", "Nom_client");
ViewBag.Id_MT = new SelectList(db.Moyens_transport, "Id_MT", "Moyen");
ViewBag.Id_prestation = new SelectList(db.Prestations, "Id_prestation", "N_prestaContrôle");
ViewBag.Id_prod = new SelectList(db.Produit, "Id_prod", "Nom_produit");
ViewBag.Id_SA = new SelectList(db.Societé_d_acconage, "Id_SA", "Nom_SA");
ViewBag.Id_ST = new SelectList(db.Societé_de_transport, "Id_ST", "Nom_ST");
ViewBag.Id_cont = new SelectList(db.Societés_de_contrôle, "Id_cont", "Nom_scont");
ViewBag.Id_TK = new SelectList(db.TK, "Id_TK", "Nom_TK");
db.Export.Add(c);
db.SaveChanges();
return Json(new { success = true, message = "Enregistré avec succès" }, JsonRequestBehavior.AllowGet);
}
else
{
db.Entry(c).State = EntityState.Modified;
ViewBag.Id_analyse = new SelectList(db.Analyse, "Id_analyse", "Id_analyse");
ViewBag.Id_client = new SelectList(db.Clients, "Id_client", "Nom_client");
ViewBag.Id_MT = new SelectList(db.Moyens_transport, "Id_MT", "Moyen");
ViewBag.Id_prestation = new SelectList(db.Prestations, "Id_prestation", "N_prestaContrôle");
ViewBag.Id_prod = new SelectList(db.Produit, "Id_prod", "Nom_produit");
ViewBag.Id_SA = new SelectList(db.Societé_d_acconage, "Id_SA", "Nom_SA");
ViewBag.Id_ST = new SelectList(db.Societé_de_transport, "Id_ST", "Nom_ST");
ViewBag.Id_cont = new SelectList(db.Societés_de_contrôle, "Id_cont", "Nom_scont");
ViewBag.Id_TK = new SelectList(db.TK, "Id_TK", "Nom_TK");
db.SaveChanges();
return Json(new { success = true, message = "Modifié avec succès" }, JsonRequestBehavior.AllowGet);
}
}
And this is the View:
@model wb01.Models.Export
@{
/**/
/**/
Layout = null;
}
@using (Html.BeginForm("AddorEdit", "Exports", FormMethod.Post, new { onsubmit = "return SubmitForm(this)" }))
{@Html.HiddenFor(model => model.Id_export)
<div class="form-group">
@Html.LabelFor(model => model.Id_prod, "Id_prod", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Id_prod", null, "Selectionner", htmlAttributes: new { @class = "form-control", size = "0,5" })
@Html.ValidationMessageFor(model => model.Id_prod, "", new { @class = "text-danger" })
</div>
</div>
<br /><br />
<div class="form-group">
@Html.LabelFor(model => model.Id_client, "Id_client", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Id_client", null, "Selectionner", htmlAttributes: new { @class = "form-control", size = "0,5" })
@Html.ValidationMessageFor(model => model.Id_client, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
@Html.LabelFor(model => model.Id_TK, "Id_TK", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Id_TK", null, "Selectionner", htmlAttributes: new { @class = "form-control", size = "0,5" })
@Html.ValidationMessageFor(model => model.Id_TK, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
@Html.LabelFor(model => model.Id_MT, "Id_MT", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Id_MT", null, "Selectionner", htmlAttributes: new { @class = "form-control", size = "0,5" })
@Html.ValidationMessageFor(model => model.Id_MT, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
@Html.LabelFor(model => model.Id_analyse, "Id_analyse", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Id_analyse", null, "Selectionner", htmlAttributes: new { @class = "form-control", size = "0,5" })
@Html.ValidationMessageFor(model => model.Id_analyse, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
@Html.LabelFor(model => model.Qté_brut, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Qté_brut, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Qté_brut, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
@Html.LabelFor(model => model.Id_ST, "Id_ST", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Id_ST", null, "Selectionner", htmlAttributes: new { @class = "form-control", size = "0,5" })
@Html.ValidationMessageFor(model => model.Id_ST, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
@Html.LabelFor(model => model.Id_SA, "Id_SA", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Id_SA", null, "Selectionner", htmlAttributes: new { @class = "form-control", size = "0,5" })
@Html.ValidationMessageFor(model => model.Id_SA, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
@Html.LabelFor(model => model.Comment_exp, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Comment_exp, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Comment_exp, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
@Html.LabelFor(model => model.Id_prestation, "Id_prestation", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Id_prestation", null, "Selectionner", htmlAttributes: new { @class = "form-control", size = "0,5" })
@Html.ValidationMessageFor(model => model.Id_prestation, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div class="form-group">
@Html.LabelFor(model => model.Id_cont, "Id_cont", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Id_cont", null, "Selectionner", htmlAttributes: new { @class = "form-control", size = "0,5" })
@Html.ValidationMessageFor(model => model.Id_cont, "", new { @class = "text-danger" })
</div>
</div>
<br />
<div>
<input type="submit" value="Envoyer" class="btn btn-primary" />
<input type="reset" value="Reset" class="btn btn-warning" />
</div>
}
The method is called from a Jquery function in my Index View; I need your help and thanks in advance.