0

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.

M.LOOK
  • 19
  • 2
  • 8
  • Your use of `DropDownList()` where the 2nd parameter is `null` will never give you correct 2-way model binding. To code it correctly, pass the `SelectList` in the 2nd parameter, and the name of the `SelectList` cannot be the same name as the property - refer [this Q/A](https://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) for the correct code (and do not use data models in your view) –  Apr 10 '18 at 12:40
  • Thanks, it works now . – M.LOOK Apr 10 '18 at 13:54
  • Thanks, it works now .Just when I use the `[DataType(DataType.Date)]` for `@Html.EditorFor(x => x.Date_d_arrivée, new { htmlAttributes = new { @class = "form-control" } })` it shows without data, just the format of date. – M.LOOK Apr 10 '18 at 14:27
  • Then I guess you have [this problem](https://stackoverflow.com/questions/31097748/date-does-not-display-from-model-on-html-input-type-date/31097864#31097864). If not ask a new question explaining the problem and showing the relevant code –  Apr 10 '18 at 23:11
  • Perfect, Thank you very much Stephen . – M.LOOK Apr 11 '18 at 08:04

0 Answers0