我的问题是当我想在我的数据表中编辑一行时,带有下拉选项的字段显示defaut值而不是所选元素的值,但它显示没有下拉列表的字段的正确值。 这是我的编辑方法:
[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);
}
}
这就是视图:
@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>
}
从我的索引视图中的Jquery函数调用该方法;我需要你的帮助,并提前致谢。