How to save the selected value of dropdownlist in DBhttp://forums.asp.net/t/1554759.aspx/1?How+to+save+the+selected+value+of+dropdownlist+in+DBWed, 12 May 2010 17:40:45 -040015547593820011http://forums.asp.net/p/1554759/3820011.aspx/1?How+to+save+the+selected+value+of+dropdownlist+in+DBHow to save the selected value of dropdownlist in DB <p>Hi everyone!!</p> <p><br> </p> <p>I am usin MVC 1.0 anc C#.</p> <p>I want to use a dropdownlist to display some data, and select one of them, then press the button &quot;Create&quot; and the value goes to the DB.</p> <p>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 !!</p> <p>Please if Some one has a simple solution, i am new developper in MVC, so dont be Harsh ;)</p> <p><br> </p> <p>here is the code:</p> <p><b>for the view :</b></p> <p><br> </p> <p><pre class="prettyprint">&lt;p&gt; &lt;label for=&quot;id_imputation&quot;&gt;Imputations:&lt;/label&gt; &lt;%= Html.DropDownList(&quot;imputations&quot;, (IEnumerable&lt;SelectListItem&gt;)ViewData[&quot;imputations&quot;])%&gt; &lt;/p&gt; &lt;p&gt; &lt;input type=&quot;submit&quot; value=&quot;Create&quot; /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;% } %&gt; &lt;div&gt; &lt;%=Html.ActionLink(&quot;Back to List&quot;, &quot;Index&quot;) %&gt; &lt;/div&gt; &lt;/asp:Content&gt;</pre><br> </p><p><br></p><p><br></p><p><b>and the COntroller :</b></p><p><br></p><p><pre class="prettyprint"> 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(); } } </pre><br> </p> <p>Best regards !</p> <p>and thanks a lot !<br> </p> 2010-05-05T15:59:41-04:003820103http://forums.asp.net/p/1554759/3820103.aspx/1?Re+How+to+save+the+selected+value+of+dropdownlist+in+DBRe: How to save the selected value of dropdownlist in DB <p><span><span>&nbsp;T_Pointage_Materiel has a property named &quot;imputations&quot;, like </span> </span></p> <p><span>&nbsp; &lt;%=&nbsp;Html.DropDownList(<span>&quot;<i><b>imputations</b></i>&quot;</span><span>,&nbsp;(IEnumerable&lt;SelectListItem&gt;)ViewData[</span><span>&quot;imputations&quot;</span><span>])%&gt;&nbsp; </span></span></p> <p><br> </p> <p><span><span>?<br> </span></span></p> 2010-05-05T16:46:15-04:003820110http://forums.asp.net/p/1554759/3820110.aspx/1?Re+How+to+save+the+selected+value+of+dropdownlist+in+DBRe: How to save the selected value of dropdownlist in DB <p>When you're passing a model from a form to the controller, the dropdown will already have updated the class, providing the binding was done properly.<br> </p> <p>assuming your <span><span>PMACreer class has a variable </span></span><span><span>imputationsId, then that is what you should be naming your dropdown list.</span></span></p> <p><span>&lt;%=&nbsp;Html.DropDownList(<span>&quot;<b>imputationsId</b>&quot;</span><span>,&nbsp;(IEnumerable&lt;SelectListItem&gt;)ViewData[</span><span>&quot;imputations&quot;</span><span>])%&gt;&nbsp; </span></span></p> <p><span><span>that is the desired method.&nbsp; While I admire your persistance, let's also not forget guys &amp; gals that MVC is built on ASP.NET &amp; HTML.</span></span></p> <p><span><span>You could have done </span></span>Request.Form[&quot;controlName&quot;].ToString() to also get the value.</p> <p><br> </p> <p>ControlId's need to be named the same as the Model attribute they bind to ...<br> </p> 2010-05-05T16:49:17-04:003821363http://forums.asp.net/p/1554759/3821363.aspx/1?Re+How+to+save+the+selected+value+of+dropdownlist+in+DBRe: How to save the selected value of dropdownlist in DB <p>Hi !</p> <p>i fixed some lines , the code :</p> <p><br> </p> <p><span class="Apple-style-span" style="font-weight:bold">the controller code :</span></p> <p><br> </p> <p><br> </p> <pre class="prettyprint">public ActionResult Create() { IEnumerable&lt;SelectListItem&gt; items = _db.T_Materiel.Select(c =&gt; new SelectListItem { Value = c.Num_anael.ToString(), Text = c.Libelle }); ViewData[&quot;imputations&quot;] = items; return View(); } // // POST: /PointageMateriel/Create [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create([Bind(Exclude = &quot;id_pointage&quot;)] T_Pointage_Materiel PMACreer) { try { // TODO: Add insert logic here UpdateModel(PMACreer); _db.AddToT_Pointage_Materiel(PMACreer); _db.SaveChanges(); return RedirectToAction(&quot;Index&quot;); } catch { return View(); } }</pre><p><br><br></p><p><br></p><p><br></p><p>the View Code:</p><p><br></p><p><br></p><p>...</p><p><br></p><pre class="prettyprint">&lt;p&gt; &lt;label for="id_imputation"&gt;Imputations:&lt;/label&gt; &lt;%= Html.DropDownList("Num_anael", (IEnumerable&lt;SelectListItem&gt;)ViewData["imputations"])%&gt; &lt;/p&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;% } %&gt; &lt;div&gt; &lt;%=Html.ActionLink("Back to List", "Index") %&gt; &lt;/div&gt; &lt;/asp:Content&gt;</pre> <p><br> <br> </p> <p>BUT , and there is always this #!§,% &quot;but&quot; , this returns an error:</p> <p>&lt;&lt;L'exception System.NotSupportedException n'a pas été gérée par le code utilisateur&nbsp;&nbsp;Message=&quot;LINQ to Entities ne reconnaît pas la méthode « System.String ToString() », et cette dernière ne peut pas être traduite en expression de magasin.&quot;&nbsp;&nbsp;Source=&quot;System.Data.Entity&quot;&nbsp;&nbsp;StackTrace:&nbsp;&nbsp; &nbsp; &nbsp; à System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.DefaultTranslator.Translate(ExpressionConverter parent,MethodCallExpression call)&nbsp;&nbsp; &nbsp; &nbsp; à System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)&nbsp;&nbsp; &nbsp; &nbsp; à System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)&nbsp;&nbsp; &nbsp; &nbsp; à System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)&nbsp;&nbsp; &nbsp; &nbsp; à System.Data.Objects.ELinq.ExpressionConverter.MemberInitTranslator.TypedTranslate(ExpressionConverter parent, MemberInitExpression linq)&nbsp;&nbsp; &nbsp; &nbsp; à System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expressionlinq)&nbsp;&nbsp; &nbsp; &nbsp; à System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) ...</p> &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input, DbExpressionBinding&amp; binding)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression&amp; source, DbExpressionBinding&amp; sourceBinding, DbExpression&amp; lambda)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SelectTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Data.Objects.ELinq.ExpressionConverter.Convert()&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable&lt;T&gt;.GetEnumerator()&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.Mvc.Html.SelectExtensions.SelectInternal(HtmlHelper htmlHelper, String optionLabel, String name, IEnumerable`1 selectList, Boolean allowMultiple, IDictionary`2 htmlAttributes)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.Mvc.Html.SelectExtensions.DropDownList(HtmlHelper htmlHelper, String name, IEnumerable`1 selectList)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à ASP.views_pointagemateriel_create_aspx.__RenderContent2(HtmlTextWriter __w, Control parameterContainer) dans c:\Documents and Settings\Administrateur\Mes documents\Visual Studio 2008\Projects\DSSCOLAS2\DSSCOLAS2\Views\PointageMateriel\Create.aspx:ligne 54&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.UI.Control.Render(HtmlTextWriter writer)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.UI.Control.RenderControl(HtmlTextWriter writer)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) dans c:\Documents and Settings\Administrateur\Mes documents\Visual Studio 2008\Projects\DSSCOLAS2\DSSCOLAS2\Views\Shared\Site.Master:ligne 72&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.UI.Control.Render(HtmlTextWriter writer)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.UI.Control.RenderControl(HtmlTextWriter writer)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.UI.Page.Render(HtmlTextWriter writer)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.UI.Control.RenderControl(HtmlTextWriter writer)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp; &nbsp; &nbsp; à System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)&lt;/div&gt; &lt;div style=&quot;position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;&quot; id=&quot;_mcePaste&quot;&gt;&nbsp;&nbsp;InnerException:&nbsp;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; <p>&gt;&gt;</p> <p><br> </p> <p>What is the problem here ? wow , i think that this framework was not a good deal, even a dropdowlist is so difficult to put on ! What about using textbox instead of dropdownlist, can u pleasegive me the solution, i am lacking time !</p> <p><br> </p> <p>thanks for the help !</p> <p></p> 2010-05-06T09:34:09-04:003821417http://forums.asp.net/p/1554759/3821417.aspx/1?Re+How+to+save+the+selected+value+of+dropdownlist+in+DBRe: How to save the selected value of dropdownlist in DB <p></p> <blockquote><span class="icon-blockquote"></span> <h4>sjfs00</h4> &nbsp;Value&nbsp;=&nbsp;c.Num_anael.ToString(),&nbsp;&nbsp;</blockquote> <p></p> <p>There are some clr methods that are not supported(http://msdn.microsoft.com/en-us/library/bb738681.aspx) ... like <br> </p> <p></p> <blockquote><span class="icon-blockquote"></span> <h4>sjfs00</h4> Value =c.Num_anael</blockquote> <p></p> <p>you can do like this - by creating new list<br> </p> <ol start="1"> <li><span>&nbsp;var&nbsp; items&nbsp;=&nbsp;_db.T_Materiel.Select(c).ToList&lt; Put here type of c&gt;;</span></li><li><span>IEnumerable&lt;SelectListItem&gt;&nbsp;itemsSelect =new </span>List&lt;<span>SelectListItem&gt;</span><br> <span></span></li><li><span>foreach(var item in items)</span></li><li><span>{</span><span>itemsSelect .Add(new <span>SelectListItem&nbsp;&nbsp;</span></span> </li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;</span> </li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Value&nbsp;=&nbsp;c.Num_anael.ToString(),&nbsp;&nbsp;</span> </li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Text&nbsp;=&nbsp;&nbsp;c.Libelle&nbsp;&nbsp;</span> </li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});&nbsp; <br> </span></li><li><span>}<br> <br> <br> </span></li></ol> 2010-05-06T10:10:30-04:003821443http://forums.asp.net/p/1554759/3821443.aspx/1?Re+How+to+save+the+selected+value+of+dropdownlist+in+DBRe: How to save the selected value of dropdownlist in DB <p><br> </p> <p>i did as you said :&nbsp;</p> <p><br> </p> <p></p> <pre class="prettyprint">public ActionResult Create() { var items = _db.T_Materiel.Select(c).ToList&lt;T_Materiel &gt;; IEnumerable&lt;SelectListItem&gt; itemsSelect =new List&lt;SelectListItem&gt; foreach(var item in items) {itemsSelect.Add(new SelectListItem { Value = c.Num_anael.ToString(), Text = c.Libelle }); }</pre> <p><br> BUT : it says that it doesn't recognize &quot;c&quot;</p> <p>and &quot;foreach iz underligned&quot; it says: &quot;Une expression new exige que type soit suivi de (), [] ou {}&quot; translation: &quot;an expression new demand type to be followed by (), [] ou {}&quot;</p> <p><br> </p> <p><b>IMAGE:</b></p> <p><br> </p> <p><b><img src="http://img40.imageshack.us/img40/853/errorcj.png" width="1177" height="544"></b></p> <p></p> 2010-05-06T10:29:17-04:003821586http://forums.asp.net/p/1554759/3821586.aspx/1?Re+How+to+save+the+selected+value+of+dropdownlist+in+DBRe: How to save the selected value of dropdownlist in DB <p>sorry , encore 4 modifs :<br> </p> <p><br> </p> <p><span>&nbsp;var&nbsp;&nbsp;items&nbsp;=&nbsp;_db.T_Materiel.Select(c<u><i><b>=&gt;c</b></i></u>).ToList&lt;T_Materiel&nbsp;&gt;;&nbsp; </span></p> <p><br> </p> <p><span>IEnumerable&lt;SelectListItem&gt;&nbsp;itemsSelect&nbsp;=<span>new</span><span>&nbsp;List&lt;SelectListItem&gt;<u><i><b> ;</b></i></u></span></span></p> <p><br> </p> <p><span>Value&nbsp;= <u><i><b>item</b></i></u>.Num_anael.ToString(),&nbsp;&nbsp;&nbsp;&nbsp;</span> <span> </span></p> <p><br> </p> <p><span>Text&nbsp;=&nbsp; <u><i><b>item</b></i></u>.Libelle&nbsp;&nbsp;&nbsp; <br> </span></p> <ol start="1"> </ol> 2010-05-06T11:38:05-04:003821786http://forums.asp.net/p/1554759/3821786.aspx/1?Re+How+to+save+the+selected+value+of+dropdownlist+in+DBRe: How to save the selected value of dropdownlist in DB <p><img src="http://forums.asp.net/tiny_mce/jscripts/tiny_mce/plugins/emotions/img/smiley-embarassed.gif" alt="Embarassed" title="Embarassed" border="0">&nbsp;Still got problems, here is thescreenshot ... if you can show me what to do, because at this level i am just followig what you say ! :D</p> <p><br> </p> <p><img src="http://img140.imageshack.us/img140/8636/error2r.png" width="1174" height="484"></p> 2010-05-06T13:36:33-04:003822140http://forums.asp.net/p/1554759/3822140.aspx/1?Re+How+to+save+the+selected+value+of+dropdownlist+in+DBRe: How to save the selected value of dropdownlist in DB <p></p> <blockquote><span class="icon-blockquote"></span> <h4>sjfs00</h4> <p></p> <p><img src="http://forums.asp.net/tiny_mce/jscripts/tiny_mce/plugins/emotions/img/smiley-embarassed.gif" alt="Embarassed" title="Embarassed" border="0">&nbsp;Still got problems, here is thescreenshot ... if you can show me what to do, because at this level i am just followig what you say ! :D</p> <p></p> </blockquote> <p></p> <p>And I apologize for the errors : </p> <p><br> </p> <p><span>var&nbsp;&nbsp;items&nbsp;=&nbsp;_db.T_Materiel.Select(c).ToList&lt;T_Materiel&nbsp;&gt;<b>();&nbsp; </b></span></p> <p><span><b>List</b>&lt;SelectListItem&gt;&nbsp;itemsSelect&nbsp;=<span>new</span><span>&nbsp;List&lt;SelectListItem&gt;<u><i><b>;<br> </b></i></u></span></span></p> 2010-05-06T17:45:59-04:003823042http://forums.asp.net/p/1554759/3823042.aspx/1?Re+How+to+save+the+selected+value+of+dropdownlist+in+DBRe: How to save the selected value of dropdownlist in DB <p>Hi, good morning !</p> <p><br> </p> <p>I adjusted the code as you said, and know nothing is underlined in the controller, accohererding to the intellisense, it's perfect.</p> <p>But i think there is something missing, you didn't use the Viewdata to grap information, and then send it to the dropdownlist.</p> <p>And when i hit F5, there is an error when trying to use the action &quot;Create&quot; , it says something about Viewdata:</p> <p><img src="http://img8.imageshack.us/img8/7451/error3f.png" width="1280" height="800"></p> <p><br> </p> <p><br> </p> <ul> <li><i>just for information, even if it s too late. : The field we want to access using the drop downlist, is a foreign key.</i></li></ul> <p><br> </p> <p><i>Thanks a lot for your interest Sir !!</i></p> 2010-05-07T07:58:13-04:003823061http://forums.asp.net/p/1554759/3823061.aspx/1?Re+How+to+save+the+selected+value+of+dropdownlist+in+DBRe: How to save the selected value of dropdownlist in DB <p>did you put</p> <p><span><span>ViewData[</span><span>&quot;imputations&quot;</span><span>] =</span></span><span>itemsSelect ;</span></p> <p><span>?<br> </span></p> 2010-05-07T08:08:18-04:003823151http://forums.asp.net/p/1554759/3823151.aspx/1?Re+How+to+save+the+selected+value+of+dropdownlist+in+DBRe: How to save the selected value of dropdownlist in DB <p>Thanks a lot sir !</p> <p><br> </p> <p>it seems to work , but actually not @ 100%</p> <p>the code is perfect for view &amp; 1/2 controller,</p> <p>the dropdownlist, loads data and i can view the values i want, but when i click on the &quot;Create Button&quot; &nbsp;an error appears on the browser:</p> <p><img src="http://img63.imageshack.us/img63/30/error404.png" width="486" height="273"></p> <p><br> </p> <p>i think it's due to the POST Action !</p> <p>Because i didn't mention anything concerning the 2 fields : (imputation &#43; materiel)</p> <p>here is the code Of the entire Action (Get &#43; POST)</p> <p></p> <pre class="prettyprint">public ActionResult Create() { var items = _db.T_imputation.Select(c=&gt;c).ToList&lt;T_imputation&gt;(); List&lt;SelectListItem&gt; itemsSelect =new List&lt;SelectListItem&gt;(); foreach(var item in items) {itemsSelect.Add(new SelectListItem { Value = item.id_imputation.ToString(), Text = item.Libelle }); } ViewData[&quot;imputations&quot;] = itemsSelect; var items2 = _db.T_Materiel.Select(c =&gt; c).ToList&lt;T_Materiel&gt;(); List&lt;SelectListItem&gt; items2Select = new List&lt;SelectListItem&gt;(); foreach (var item in items2) { items2Select.Add(new SelectListItem { Value = item.Num_anael.ToString(), Text = item.Libelle }); } ViewData[&quot;Materiel&quot;] = 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(&quot;Index&quot;); } catch { return View(); } }</pre> <p><br> Thanks e lot Sir for your Help !&nbsp;</p> <p></p> 2010-05-07T09:04:54-04:003823170http://forums.asp.net/p/1554759/3823170.aspx/1?Re+How+to+save+the+selected+value+of+dropdownlist+in+DBRe: How to save the selected value of dropdownlist in DB <p></p> <blockquote><span class="icon-blockquote"></span> <h4>sjfs00</h4> i think it's due to the POST Action !</blockquote> <p></p> <p>Yes- but this is another error, related to routes, not to the dropdown. </p> <p>What's the code for submit button and what's the code for the form ?<br> </p> 2010-05-07T09:21:17-04:003823246http://forums.asp.net/p/1554759/3823246.aspx/1?Re+How+to+save+the+selected+value+of+dropdownlist+in+DBRe: How to save the selected value of dropdownlist in DB <p><br> </p> <p><br> </p> <p>I confess not understanding anything about what &nbsp;u ve just said , Routes?! &nbsp;i taught i was near :(</p> <p>euh , okay , you asked me to show you the code for the submit button:&nbsp;</p> <p><br> </p> <p><br> </p> <p></p> <pre class="prettyprint">&lt;asp:Content ID=&quot;Content2&quot; ContentPlaceHolderID=&quot;MainContent&quot; runat=&quot;server&quot;&gt; &lt;h2&gt;Create&lt;/h2&gt; &lt;%= Html.ValidationSummary(&quot;Create was unsuccessful. Please correct the errors and try again.&quot;) %&gt; &lt;%--&lt;% using (Html.BeginForm()) {%&gt;--%&gt; &lt;% using (Html.BeginForm(&quot;&lt;create&gt;&quot;, &quot;&lt;PointageMateriel&gt;&quot;)) { %&gt; &lt;fieldset&gt; &lt;legend&gt;Fields&lt;/legend&gt; &lt;p&gt; &lt;label for=&quot;id_pointage&quot;&gt;id_pointage:&lt;/label&gt; &lt;%= Html.TextBox(&quot;id_pointage&quot;) %&gt; &lt;%= Html.ValidationMessage(&quot;id_pointage&quot;, &quot;*&quot;) %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for=&quot;Quantite&quot;&gt;Quantite:&lt;/label&gt; &lt;%= Html.TextBox(&quot;Quantite&quot;) %&gt; &lt;%= Html.ValidationMessage(&quot;Quantite&quot;, &quot;*&quot;) %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for=&quot;Compteur&quot;&gt;Compteur:&lt;/label&gt; &lt;%= Html.TextBox(&quot;Compteur&quot;) %&gt; &lt;%= Html.ValidationMessage(&quot;Compteur&quot;, &quot;*&quot;) %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for=&quot;Date_Pointage&quot;&gt;Date_Pointage:&lt;/label&gt; &lt;%= Html.TextBox(&quot;Date_Pointage&quot;) %&gt; &lt;%= Html.ValidationMessage(&quot;Date_Pointage&quot;, &quot;*&quot;) %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for=&quot;Tarif_Applique&quot;&gt;Tarif_Applique:&lt;/label&gt; &lt;%= Html.TextBox(&quot;Tarif_Applique&quot;) %&gt; &lt;%= Html.ValidationMessage(&quot;Tarif_Applique&quot;, &quot;*&quot;) %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for=&quot;Abattement_Applique&quot;&gt;Abattement_Applique:&lt;/label&gt; &lt;%= Html.TextBox(&quot;Abattement_Applique&quot;) %&gt; &lt;%= Html.ValidationMessage(&quot;Abattement_Applique&quot;, &quot;*&quot;) %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for=&quot;Bloque&quot;&gt;Bloque:&lt;/label&gt; &lt;%= Html.TextBox(&quot;Bloque&quot;) %&gt; &lt;%= Html.ValidationMessage(&quot;Bloque&quot;, &quot;*&quot;) %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for=&quot;id_imputation&quot;&gt;Imputations:&lt;/label&gt; &lt;%= Html.DropDownList(&quot;imputations&quot;)%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for=&quot;num_anael&quot;&gt;Matériel concerné:&lt;/label&gt; &lt;%= Html.DropDownList(&quot;Materiel&quot;)%&gt; &lt;/p&gt; &lt;p&gt; &lt;input type=&quot;submit&quot; value=&quot;Create&quot; /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;% } %&gt; &lt;div&gt; &lt;%=Html.ActionLink(&quot;Back to List&quot;, &quot;Index&quot;) %&gt; &lt;/div&gt; &lt;/asp:Content&gt;</pre> <p><br> </p> <p>----------------------------------------------------------------</p> <p><br> </p> <p>is there any file you d like to see ? <img src="http://forums.asp.net/tiny_mce/jscripts/tiny_mce/plugins/emotions/img/smiley-embarassed.gif" alt="Embarassed" title="Embarassed" border="0"></p> <p>euh i don't have any idea about what are &nbsp;the routes for ...</p> <p><br> </p> <p>thanks again and again sir for your valuable &nbsp;help .</p> <p>can i suggest you a page where there is an example of saving a dropdownlist value in DB:</p> <p><a href="http://www.mikesdotnetting.com/Article/109/ASP.NET-MVC-Entity-Framework-One-to-Many-and-Many-to-Many-INSERTS">http://www.mikesdotnetting.com/Article/109/ASP.NET-MVC-Entity-Framework-One-to-Many-and-Many-to-Many-INSERTS</a></p> <p>in this blog, they added some code lines in the POST, i &quot;think&quot; that we are missing this in my code...</p> <p><br> </p> <p>thanks Sir !</p> <p></p> 2010-05-07T10:16:40-04:003823268http://forums.asp.net/p/1554759/3823268.aspx/1?Re+How+to+save+the+selected+value+of+dropdownlist+in+DBRe: How to save the selected value of dropdownlist in DB <p></p> <blockquote><span class="icon-blockquote"></span> <h4>sjfs00</h4> <span class="keyword">using</span><span>&nbsp;(Html.BeginForm(</span><span class="string">&quot;<b>&lt;</b>create&gt;&quot;</span><span>,&nbsp;</span><span class="string">&quot;&lt;PointageMateriel&gt;&quot;</span><span>))</span></blockquote> <p></p> <p><span>&nbsp;<span>using</span><span>&nbsp;(Html.BeginForm(</span><span>&quot;create&quot;</span><span>,&nbsp;</span><span>&quot;PointageMateriel&quot;</span><span>))</span></span></p> 2010-05-07T10:50:42-04:003823311http://forums.asp.net/p/1554759/3823311.aspx/1?Re+How+to+save+the+selected+value+of+dropdownlist+in+DBRe: How to save the selected value of dropdownlist in DB <p><br> </p> <p><br> </p> <p>hum hum , it becomes embarassing, sorry Sir for that . <img src="http://forums.asp.net/tiny_mce/jscripts/tiny_mce/plugins/emotions/img/smiley-embarassed.gif" alt="Embarassed" title="Embarassed" border="0"></p> <p>but now with <span class="Apple-style-span" style="text-decoration:underline"> using (Html.BeginForm(&quot;create&quot;, &quot;PointageMateriel&quot;)) </span>&nbsp;the submit button click tries to do sth, but an error occurs saying: there is no viewdata item with key &quot;...&quot; .... here is the screen shot:</p> <p>&nbsp;<img src="http://img52.imageshack.us/img52/8174/errornoviewdataitem.png" width="1280" height="800"></p> <p><br> </p> <p><br> </p> <p>the code in the controlleris as followeing:&nbsp;</p> <p><br> </p> <pre class="prettyprint">public ActionResult Create() { var items = _db.T_imputation.Select(c=&gt;c).ToList&lt;T_imputation&gt;(); List&lt;SelectListItem&gt; itemsSelect =new List&lt;SelectListItem&gt;(); foreach(var item in items) {itemsSelect.Add(new SelectListItem { Value = item.id_imputation.ToString(), Text = item.Libelle }); } ViewData[&quot;imputations&quot;] = itemsSelect; var items2 = _db.T_Materiel.Select(c =&gt; c).ToList&lt;T_Materiel&gt;(); List&lt;SelectListItem&gt; items2Select = new List&lt;SelectListItem&gt;(); foreach (var item in items2) { items2Select.Add(new SelectListItem { Value = item.Num_anael.ToString(), Text = item.Libelle }); } ViewData[&quot;Materiel&quot;] = 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(&quot;Index&quot;); } catch { return View(); } }</pre><p><br></p><p><br></p><p>and the view :</p><p><br></p><p></p><pre class="prettyprint">&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;DSSCOLAS2.Models.T_Pointage_Materiel&gt;" %&gt; &lt;asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"&gt; Create &lt;/asp:Content&gt; &lt;asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"&gt; &lt;h2&gt;Create&lt;/h2&gt; &lt;%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %&gt; &lt;% using (Html.BeginForm("create", "PointageMateriel")) { %&gt; &lt;fieldset&gt; &lt;legend&gt;Fields&lt;/legend&gt; &lt;p&gt; &lt;label for="id_pointage"&gt;id_pointage:&lt;/label&gt; &lt;%= Html.TextBox("id_pointage") %&gt; &lt;%= Html.ValidationMessage("id_pointage", "*") %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="Quantite"&gt;Quantite:&lt;/label&gt; &lt;%= Html.TextBox("Quantite") %&gt; &lt;%= Html.ValidationMessage("Quantite", "*") %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="Compteur"&gt;Compteur:&lt;/label&gt; &lt;%= Html.TextBox("Compteur") %&gt; &lt;%= Html.ValidationMessage("Compteur", "*") %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="Date_Pointage"&gt;Date_Pointage:&lt;/label&gt; &lt;%= Html.TextBox("Date_Pointage") %&gt; &lt;%= Html.ValidationMessage("Date_Pointage", "*") %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="Tarif_Applique"&gt;Tarif_Applique:&lt;/label&gt; &lt;%= Html.TextBox("Tarif_Applique") %&gt; &lt;%= Html.ValidationMessage("Tarif_Applique", "*") %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="Abattement_Applique"&gt;Abattement_Applique:&lt;/label&gt; &lt;%= Html.TextBox("Abattement_Applique") %&gt; &lt;%= Html.ValidationMessage("Abattement_Applique", "*") %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="Bloque"&gt;Bloque:&lt;/label&gt; &lt;%= Html.TextBox("Bloque") %&gt; &lt;%= Html.ValidationMessage("Bloque", "*") %&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="id_imputation"&gt;Imputations:&lt;/label&gt; &lt;%= Html.DropDownList("imputations")%&gt; &lt;/p&gt; &lt;p&gt; &lt;label for="num_anael"&gt;Matériel concerné:&lt;/label&gt; &lt;%= Html.DropDownList("Materiel")%&gt; &lt;/p&gt; &lt;p&gt; &lt;input type="submit" value="Create" /&gt; &lt;/p&gt; &lt;/fieldset&gt; &lt;% } %&gt; &lt;div&gt; &lt;%=Html.ActionLink("Back to List", "Index") %&gt; &lt;/div&gt; &lt;/asp:Content&gt; </pre> <p><br> <br> </p> <p></p> <p><br> </p> <p>I hope you re not bored by my case ... &nbsp;<img src="http://forums.asp.net/tiny_mce/jscripts/tiny_mce/plugins/emotions/img/smiley-embarassed.gif" alt="Embarassed" title="Embarassed" border="0"></p> <p>i really need this to work.</p> <p><br> </p> <p><i><b>Kind Regards !</b></i></p> 2010-05-07T11:21:50-04:003824201http://forums.asp.net/p/1554759/3824201.aspx/1?Re+How+to+save+the+selected+value+of+dropdownlist+in+DBRe: How to save the selected value of dropdownlist in DB <p>I suggest the following for&nbsp; <span></span></p> <p><span>[AcceptVerbs(HttpVerbs.Post)]&nbsp;&nbsp;</span> <span></span></p> <p><span><span>public</span><span>&nbsp;ActionResult&nbsp;Create(&nbsp;T_Pointage_Materiel&nbsp;PMACreer)&nbsp; <br> </span></span></p> <ol start="1"> </ol> <p><br> </p> <li class="alt"><span><span class="keyword">try</span><span>&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;UpdateModel(PMACreer);&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_db.AddToT_Pointage_Materiel(PMACreer);&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_db.SaveChanges();&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">return</span><span>&nbsp;RedirectToAction(</span><span class="string">&quot;Index&quot;</span><span>);&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">catch</span><span>(Exception ex)<br> </span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;</span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ModelState.AddModelError(&quot;&quot;, ex.Message);</span></li><li class=""><span>//copy the code to fill </span><span>ViewData[<span>&quot;imputations&quot;</span><span>]&nbsp;=&nbsp;itemsSelect;&nbsp;&nbsp; and </span></span><span>&nbsp;&nbsp; ViewData[<span>&quot;Materiel&quot;</span><span>]&nbsp;=&nbsp;items2Select;&nbsp; </span> </span></li><li class=""><span><span>//or,better ,put in a function<br> </span></span></li><li class="alt"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword">return</span><span>&nbsp;View();&nbsp;&nbsp;</span></span></li><li class=""><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span> <p><br> </p> </li>2010-05-07T21:09:19-04:003825340http://forums.asp.net/p/1554759/3825340.aspx/1?Re+How+to+save+the+selected+value+of+dropdownlist+in+DBRe: How to save the selected value of dropdownlist in DB <p>Thank you first of all for your interest to my case, and for being so helpful, and to be &nbsp;the only one who cared so much</p> <p><br> </p> <p>&nbsp;I put the code you told me in the POST,</p> <p>but the :</p> <p>&quot; </p> <pre class="prettyprint">[AcceptVerbs(HttpVerbs.Post)] public ActionResult Create( T_Pointage_Materiel PMACreer) { try { UpdateModel(PMACreer); _db.AddToT_Pointage_Materiel(PMACreer); _db.SaveChanges(); return RedirectToAction(&quot;Index&quot;); } catch { //ModelState.AddModelError(&quot;&quot;,Ex.message); var items = _db.T_imputation.Select(c =&gt; c).ToList&lt;T_imputation&gt;(); List&lt;SelectListItem&gt; itemsSelect = new List&lt;SelectListItem&gt;(); foreach (var item in items) { itemsSelect.Add(new SelectListItem { Value = item.id_imputation.ToString(), Text = item.Libelle }); } ViewData[&quot;itemsSelect&quot;] = itemsSelect; var items2 = _db.T_Materiel.Select(c =&gt; c).ToList&lt;T_Materiel&gt;(); List&lt;SelectListItem&gt; items2Select = new List&lt;SelectListItem&gt;(); foreach (var item in items2) { items2Select.Add(new SelectListItem { Value = item.Num_anael.ToString(), Text = item.Libelle }); } ViewData[&quot;Materiel&quot;] = items2Select; return View(); } }</pre> <p></p> 2010-05-08T18:37:26-04:003825351http://forums.asp.net/p/1554759/3825351.aspx/1?Re+How+to+save+the+selected+value+of+dropdownlist+in+DBRe: How to save the selected value of dropdownlist in DB <p>for help : i read in a forum that they put this in the post :</p> <p></p> <pre class="prettyprint">var articleTypeId = Request.Form[&quot;ArticleTypeID&quot;]; var categoryId = Request[&quot;CategoryID&quot;];</pre> <p><br> </p> <p><br> </p> <p>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 ...</p> <p>am nnot sure of what i say ..</p> <p><br> </p> <p>Thanks !</p> <p></p> 2010-05-08T18:50:23-04:003825358http://forums.asp.net/p/1554759/3825358.aspx/1?Re+How+to+save+the+selected+value+of+dropdownlist+in+DBRe: How to save the selected value of dropdownlist in DB <p></p> <blockquote><span class="icon-blockquote"></span> <h4>sjfs00</h4> <p></p> <p>but the :</p> <p>&quot; </p> <pre class="prettyprint">ModelState.AddModelError(&quot;&quot;, Ex.message);</pre> <p></p> </blockquote> <p></p> <p>You did not put, instead of</p> <p>cath</p> <p>the source :<br> </p> <p><span>&nbsp;<span>catch</span><span>&nbsp; <b>(Ex as Exception) </b></span></span></p> <p><br> </p> <p><span><span>And this is the cause that you do not see any error on </span></span></p> <p><span><span></p> <blockquote><span class="icon-blockquote"></span> <h4>sjfs00</h4> <span>when&nbsp;i&nbsp;click&nbsp;on&nbsp;</span><span class="string">&quot;Creat&quot;</span><span>&nbsp;button,&nbsp;it&nbsp;relods&nbsp;the&nbsp;page&nbsp;with&nbsp;the&nbsp;data&nbsp;i&nbsp;entred,&nbsp;but&nbsp;,&nbsp;nothing&nbsp;happens&nbsp;to&nbsp;the&nbsp;DB.&nbsp;&nbsp;</span></blockquote> <br> </span></span> <p></p> 2010-05-08T18:57:19-04:00