hum hum , it becomes embarassing, sorry Sir for that .
but now with
using (Html.BeginForm("create", "PointageMateriel")) the submit button click tries to do sth, but an error occurs saying: there is no viewdata item with key "..." .... here is the screen shot:
the code in the controlleris as followeing:
public ActionResult Create()
{
var items = _db.T_imputation.Select(c=>c).ToList<T_imputation>();
List<SelectListItem> itemsSelect =new List<SelectListItem>();
foreach(var item in items)
{itemsSelect.Add(new SelectListItem
{
Value = item.id_imputation.ToString(),
Text = item.Libelle
});
}
ViewData["imputations"] = itemsSelect;
var items2 = _db.T_Materiel.Select(c => c).ToList<T_Materiel>();
List<SelectListItem> items2Select = new List<SelectListItem>();
foreach (var item in items2)
{
items2Select.Add(new SelectListItem
{
Value = item.Num_anael.ToString(),
Text = item.Libelle
});
}
ViewData["Materiel"] = items2Select;
return View();
}
//
// POST: /PointageMateriel/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create( T_Pointage_Materiel PMACreer)
{
try
{
UpdateModel(PMACreer);
_db.AddToT_Pointage_Materiel(PMACreer);
_db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
ignatandrei
All-Star
134521 Points
21576 Posts
Moderator
MVP
Re: How to save the selected value of dropdownlist in DB
May 07, 2010 08:08 AM|LINK
did you put
ViewData["imputations"] =itemsSelect ;
?
sjfs00
0 Points
22 Posts
Re: How to save the selected value of dropdownlist in DB
May 07, 2010 09:04 AM|LINK
Thanks a lot sir !
it seems to work , but actually not @ 100%
the code is perfect for view & 1/2 controller,
the dropdownlist, loads data and i can view the values i want, but when i click on the "Create Button" an error appears on the browser:
i think it's due to the POST Action !
Because i didn't mention anything concerning the 2 fields : (imputation + materiel)
here is the code Of the entire Action (Get + POST)
public ActionResult Create() { var items = _db.T_imputation.Select(c=>c).ToList<T_imputation>(); List<SelectListItem> itemsSelect =new List<SelectListItem>(); foreach(var item in items) {itemsSelect.Add(new SelectListItem { Value = item.id_imputation.ToString(), Text = item.Libelle }); } ViewData["imputations"] = itemsSelect; var items2 = _db.T_Materiel.Select(c => c).ToList<T_Materiel>(); List<SelectListItem> items2Select = new List<SelectListItem>(); foreach (var item in items2) { items2Select.Add(new SelectListItem { Value = item.Num_anael.ToString(), Text = item.Libelle }); } ViewData["Materiel"] = items2Select; return View(); } // // POST: /PointageMateriel/Create [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create( T_Pointage_Materiel PMACreer) { try { UpdateModel(PMACreer); _db.AddToT_Pointage_Materiel(PMACreer); _db.SaveChanges(); return RedirectToAction("Index"); } catch { return View(); } }Thanks e lot Sir for your Help !
ignatandrei
All-Star
134521 Points
21576 Posts
Moderator
MVP
Re: How to save the selected value of dropdownlist in DB
May 07, 2010 09:21 AM|LINK
Yes- but this is another error, related to routes, not to the dropdown.
What's the code for submit button and what's the code for the form ?
sjfs00
0 Points
22 Posts
Re: How to save the selected value of dropdownlist in DB
May 07, 2010 10:16 AM|LINK
I confess not understanding anything about what u ve just said , Routes?! i taught i was near :(
euh , okay , you asked me to show you the code for the submit button:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>Create</h2> <%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %> <%--<% using (Html.BeginForm()) {%>--%> <% using (Html.BeginForm("<create>", "<PointageMateriel>")) { %> <fieldset> <legend>Fields</legend> <p> <label for="id_pointage">id_pointage:</label> <%= Html.TextBox("id_pointage") %> <%= Html.ValidationMessage("id_pointage", "*") %> </p> <p> <label for="Quantite">Quantite:</label> <%= Html.TextBox("Quantite") %> <%= Html.ValidationMessage("Quantite", "*") %> </p> <p> <label for="Compteur">Compteur:</label> <%= Html.TextBox("Compteur") %> <%= Html.ValidationMessage("Compteur", "*") %> </p> <p> <label for="Date_Pointage">Date_Pointage:</label> <%= Html.TextBox("Date_Pointage") %> <%= Html.ValidationMessage("Date_Pointage", "*") %> </p> <p> <label for="Tarif_Applique">Tarif_Applique:</label> <%= Html.TextBox("Tarif_Applique") %> <%= Html.ValidationMessage("Tarif_Applique", "*") %> </p> <p> <label for="Abattement_Applique">Abattement_Applique:</label> <%= Html.TextBox("Abattement_Applique") %> <%= Html.ValidationMessage("Abattement_Applique", "*") %> </p> <p> <label for="Bloque">Bloque:</label> <%= Html.TextBox("Bloque") %> <%= Html.ValidationMessage("Bloque", "*") %> </p> <p> <label for="id_imputation">Imputations:</label> <%= Html.DropDownList("imputations")%> </p> <p> <label for="num_anael">Matériel concerné:</label> <%= Html.DropDownList("Materiel")%> </p> <p> <input type="submit" value="Create" /> </p> </fieldset> <% } %> <div> <%=Html.ActionLink("Back to List", "Index") %> </div> </asp:Content>----------------------------------------------------------------
is there any file you d like to see ?
euh i don't have any idea about what are the routes for ...
thanks again and again sir for your valuable help .
can i suggest you a page where there is an example of saving a dropdownlist value in DB:
http://www.mikesdotnetting.com/Article/109/ASP.NET-MVC-Entity-Framework-One-to-Many-and-Many-to-Many-INSERTS
in this blog, they added some code lines in the POST, i "think" that we are missing this in my code...
thanks Sir !
ignatandrei
All-Star
134521 Points
21576 Posts
Moderator
MVP
Re: How to save the selected value of dropdownlist in DB
May 07, 2010 10:50 AM|LINK
using (Html.BeginForm("create", "PointageMateriel"))
sjfs00
0 Points
22 Posts
Re: How to save the selected value of dropdownlist in DB
May 07, 2010 11:21 AM|LINK
hum hum , it becomes embarassing, sorry Sir for that .
but now with using (Html.BeginForm("create", "PointageMateriel")) the submit button click tries to do sth, but an error occurs saying: there is no viewdata item with key "..." .... here is the screen shot:
the code in the controlleris as followeing:
public ActionResult Create() { var items = _db.T_imputation.Select(c=>c).ToList<T_imputation>(); List<SelectListItem> itemsSelect =new List<SelectListItem>(); foreach(var item in items) {itemsSelect.Add(new SelectListItem { Value = item.id_imputation.ToString(), Text = item.Libelle }); } ViewData["imputations"] = itemsSelect; var items2 = _db.T_Materiel.Select(c => c).ToList<T_Materiel>(); List<SelectListItem> items2Select = new List<SelectListItem>(); foreach (var item in items2) { items2Select.Add(new SelectListItem { Value = item.Num_anael.ToString(), Text = item.Libelle }); } ViewData["Materiel"] = items2Select; return View(); } // // POST: /PointageMateriel/Create [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create( T_Pointage_Materiel PMACreer) { try { UpdateModel(PMACreer); _db.AddToT_Pointage_Materiel(PMACreer); _db.SaveChanges(); return RedirectToAction("Index"); } catch { return View(); } }and the view :
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<DSSCOLAS2.Models.T_Pointage_Materiel>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Create </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>Create</h2> <%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %> <% using (Html.BeginForm("create", "PointageMateriel")) { %> <fieldset> <legend>Fields</legend> <p> <label for="id_pointage">id_pointage:</label> <%= Html.TextBox("id_pointage") %> <%= Html.ValidationMessage("id_pointage", "*") %> </p> <p> <label for="Quantite">Quantite:</label> <%= Html.TextBox("Quantite") %> <%= Html.ValidationMessage("Quantite", "*") %> </p> <p> <label for="Compteur">Compteur:</label> <%= Html.TextBox("Compteur") %> <%= Html.ValidationMessage("Compteur", "*") %> </p> <p> <label for="Date_Pointage">Date_Pointage:</label> <%= Html.TextBox("Date_Pointage") %> <%= Html.ValidationMessage("Date_Pointage", "*") %> </p> <p> <label for="Tarif_Applique">Tarif_Applique:</label> <%= Html.TextBox("Tarif_Applique") %> <%= Html.ValidationMessage("Tarif_Applique", "*") %> </p> <p> <label for="Abattement_Applique">Abattement_Applique:</label> <%= Html.TextBox("Abattement_Applique") %> <%= Html.ValidationMessage("Abattement_Applique", "*") %> </p> <p> <label for="Bloque">Bloque:</label> <%= Html.TextBox("Bloque") %> <%= Html.ValidationMessage("Bloque", "*") %> </p> <p> <label for="id_imputation">Imputations:</label> <%= Html.DropDownList("imputations")%> </p> <p> <label for="num_anael">Matériel concerné:</label> <%= Html.DropDownList("Materiel")%> </p> <p> <input type="submit" value="Create" /> </p> </fieldset> <% } %> <div> <%=Html.ActionLink("Back to List", "Index") %> </div> </asp:Content>I hope you re not bored by my case ...
i really need this to work.
Kind Regards !
ignatandrei
All-Star
134521 Points
21576 Posts
Moderator
MVP
Re: How to save the selected value of dropdownlist in DB
May 07, 2010 09:09 PM|LINK
I suggest the following for
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create( T_Pointage_Materiel PMACreer)
sjfs00
0 Points
22 Posts
Re: How to save the selected value of dropdownlist in DB
May 08, 2010 06:37 PM|LINK
Thank you first of all for your interest to my case, and for being so helpful, and to be the only one who cared so much
I put the code you told me in the POST,
but the :
"
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Create( T_Pointage_Materiel PMACreer) { try { UpdateModel(PMACreer); _db.AddToT_Pointage_Materiel(PMACreer); _db.SaveChanges(); return RedirectToAction("Index"); } catch { //ModelState.AddModelError("",Ex.message); var items = _db.T_imputation.Select(c => c).ToList<T_imputation>(); List<SelectListItem> itemsSelect = new List<SelectListItem>(); foreach (var item in items) { itemsSelect.Add(new SelectListItem { Value = item.id_imputation.ToString(), Text = item.Libelle }); } ViewData["itemsSelect"] = itemsSelect; var items2 = _db.T_Materiel.Select(c => c).ToList<T_Materiel>(); List<SelectListItem> items2Select = new List<SelectListItem>(); foreach (var item in items2) { items2Select.Add(new SelectListItem { Value = item.Num_anael.ToString(), Text = item.Libelle }); } ViewData["Materiel"] = items2Select; return View(); } }sjfs00
0 Points
22 Posts
Re: How to save the selected value of dropdownlist in DB
May 08, 2010 06:50 PM|LINK
for help : i read in a forum that they put this in the post :
to get the selected values from the view, and store them for exampl in a variable, and then put them in the model or sth like this ...
am nnot sure of what i say ..
Thanks !
ignatandrei
All-Star
134521 Points
21576 Posts
Moderator
MVP
Re: How to save the selected value of dropdownlist in DB
May 08, 2010 06:57 PM|LINK
You did not put, instead of
cath
the source :
catch (Ex as Exception)
And this is the cause that you do not see any error on