I want to use a dropdownlist to display some data, and select one of them, then press the button "Create" and the value goes to the DB.
i tried to do sth, but all what i did, is to display the data in the dropdownlist in the view, But for saving the selected value, i ally don't know how to proceed, am on this more than 6h today, i am totalled !!
Please if Some one has a simple solution, i am new developper in MVC, so dont be Harsh ;)
sjfs00
0 Points
22 Posts
How to save the selected value of dropdownlist in DB
May 05, 2010 03:59 PM|LINK
Hi everyone!!
I am usin MVC 1.0 anc C#.
I want to use a dropdownlist to display some data, and select one of them, then press the button "Create" and the value goes to the DB.
i tried to do sth, but all what i did, is to display the data in the dropdownlist in the view, But for saving the selected value, i ally don't know how to proceed, am on this more than 6h today, i am totalled !!
Please if Some one has a simple solution, i am new developper in MVC, so dont be Harsh ;)
here is the code:
for the view :
<p> <label for="id_imputation">Imputations:</label> <%= Html.DropDownList("imputations", (IEnumerable<SelectListItem>)ViewData["imputations"])%> </p> <p> <input type="submit" value="Create" /> </p> </fieldset> <% } %> <div> <%=Html.ActionLink("Back to List", "Index") %> </div> </asp:Content>and the COntroller :
public ActionResult Create() { var impu = from im in _db.T_imputation select im.id_imputation; ViewData["imputations"] = new SelectList(impu.ToList()); ViewData.Model = new T_Pointage_Materiel(); return View(); } // // POST: /PointageMateriel/Create [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create([Bind(Exclude = "id_pointage")] T_Pointage_Materiel PMACreer) { try { // TODO: Add insert logic here UpdateModel(PMACreer); //PMACreer.T_imputation.id_imputation = ; PMACreer.T_Materiel.Num_anael = "1"; _db.AddToT_Pointage_Materiel(PMACreer); _db.SaveChanges(); ViewData["rr"] = PMACreer.Compteur; return RedirectToAction("Index"); } catch { return View(); } }Best regards !
and thanks a lot !