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 ;)
BUT , and there is always this #!§,% "but" , this returns an error:
<<L'exception System.NotSupportedException n'a pas été gérée par le code utilisateur Message="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." Source="System.Data.Entity" StackTrace:
à System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.DefaultTranslator.Translate(ExpressionConverter parent,MethodCallExpression call) à System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter
parent, MethodCallExpression linq) à System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) à System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
à System.Data.Objects.ELinq.ExpressionConverter.MemberInitTranslator.TypedTranslate(ExpressionConverter parent, MemberInitExpression linq) à System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expressionlinq)
à System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) ...
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 !
public ActionResult Create()
{
var items = _db.T_Materiel.Select(c).ToList<T_Materiel >;
IEnumerable<SelectListItem> itemsSelect =new List<SelectListItem>
foreach(var item in items)
{itemsSelect.Add(new SelectListItem
{
Value = c.Num_anael.ToString(),
Text = c.Libelle
});
}
BUT : it says that it doesn't recognize "c"
and "foreach iz underligned" it says: "Une expression new exige que type soit suivi de (), [] ou {}" translation: "an expression new demand type to be followed by (), [] ou {}"
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 !
ignatandrei
All-Star
134913 Points
21619 Posts
Moderator
MVP
Re: How to save the selected value of dropdownlist in DB
May 05, 2010 04:46 PM|LINK
T_Pointage_Materiel has a property named "imputations", like
<%= Html.DropDownList("imputations", (IEnumerable<SelectListItem>)ViewData["imputations"])%>
?
Pikesville P...
Participant
1036 Points
197 Posts
Re: How to save the selected value of dropdownlist in DB
May 05, 2010 04:49 PM|LINK
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.
assuming your PMACreer class has a variable imputationsId, then that is what you should be naming your dropdown list.
<%= Html.DropDownList("imputationsId", (IEnumerable<SelectListItem>)ViewData["imputations"])%>
that is the desired method. While I admire your persistance, let's also not forget guys & gals that MVC is built on ASP.NET & HTML.
You could have done Request.Form["controlName"].ToString() to also get the value.
ControlId's need to be named the same as the Model attribute they bind to ...
You haven't had a beer until you've learned how to make your own...
sjfs00
0 Points
22 Posts
Re: How to save the selected value of dropdownlist in DB
May 06, 2010 09:34 AM|LINK
Hi !
i fixed some lines , the code :
the controller code :
public ActionResult Create() { IEnumerable<SelectListItem> items = _db.T_Materiel.Select(c => new SelectListItem { Value = c.Num_anael.ToString(), Text = c.Libelle }); ViewData["imputations"] = items; 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); _db.AddToT_Pointage_Materiel(PMACreer); _db.SaveChanges(); return RedirectToAction("Index"); } catch { return View(); } }the View Code:
...
<p> <label for="id_imputation">Imputations:</label> <%= Html.DropDownList("Num_anael", (IEnumerable<SelectListItem>)ViewData["imputations"])%> </p> <p> <input type="submit" value="Create" /> </p> </fieldset> <% } %> <div> <%=Html.ActionLink("Back to List", "Index") %> </div> </asp:Content>BUT , and there is always this #!§,% "but" , this returns an error:
<<L'exception System.NotSupportedException n'a pas été gérée par le code utilisateur Message="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." Source="System.Data.Entity" StackTrace: à System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.DefaultTranslator.Translate(ExpressionConverter parent,MethodCallExpression call) à System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) à System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq) à System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) à System.Data.Objects.ELinq.ExpressionConverter.MemberInitTranslator.TypedTranslate(ExpressionConverter parent, MemberInitExpression linq) à System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expressionlinq) à System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) ...
<div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input, DbExpressionBinding& binding)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SelectTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Data.Objects.ELinq.ExpressionConverter.Convert()</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.Mvc.Html.SelectExtensions.SelectInternal(HtmlHelper htmlHelper, String optionLabel, String name, IEnumerable`1 selectList, Boolean allowMultiple, IDictionary`2 htmlAttributes)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.Mvc.Html.SelectExtensions.DropDownList(HtmlHelper htmlHelper, String name, IEnumerable`1 selectList)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à 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</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.UI.Control.Render(HtmlTextWriter writer)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.UI.Control.RenderControl(HtmlTextWriter writer)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à 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</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.UI.Control.Render(HtmlTextWriter writer)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.UI.Control.RenderControl(HtmlTextWriter writer)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.UI.Page.Render(HtmlTextWriter writer)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.UI.Control.RenderControl(HtmlTextWriter writer)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> à System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)</div> <div style="position: absolute; left: -10000px; top: 1038px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;" id="_mcePaste"> InnerException: </div> <div></div>>>
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 !
thanks for the help !
ignatandrei
All-Star
134913 Points
21619 Posts
Moderator
MVP
Re: How to save the selected value of dropdownlist in DB
May 06, 2010 10:10 AM|LINK
There are some clr methods that are not supported(http://msdn.microsoft.com/en-us/library/bb738681.aspx) ... like
you can do like this - by creating new list
sjfs00
0 Points
22 Posts
Re: How to save the selected value of dropdownlist in DB
May 06, 2010 10:29 AM|LINK
i did as you said :
public ActionResult Create() { var items = _db.T_Materiel.Select(c).ToList<T_Materiel >; IEnumerable<SelectListItem> itemsSelect =new List<SelectListItem> foreach(var item in items) {itemsSelect.Add(new SelectListItem { Value = c.Num_anael.ToString(), Text = c.Libelle }); }BUT : it says that it doesn't recognize "c"
and "foreach iz underligned" it says: "Une expression new exige que type soit suivi de (), [] ou {}" translation: "an expression new demand type to be followed by (), [] ou {}"
IMAGE:
ignatandrei
All-Star
134913 Points
21619 Posts
Moderator
MVP
Re: How to save the selected value of dropdownlist in DB
May 06, 2010 11:38 AM|LINK
sorry , encore 4 modifs :
var items = _db.T_Materiel.Select(c=>c).ToList<T_Materiel >;
IEnumerable<SelectListItem> itemsSelect =new List<SelectListItem> ;
Value = item.Num_anael.ToString(),
Text = item.Libelle
sjfs00
0 Points
22 Posts
Re: How to save the selected value of dropdownlist in DB
May 06, 2010 01:36 PM|LINK
ignatandrei
All-Star
134913 Points
21619 Posts
Moderator
MVP
Re: How to save the selected value of dropdownlist in DB
May 06, 2010 05:45 PM|LINK
And I apologize for the errors :
var items = _db.T_Materiel.Select(c).ToList<T_Materiel >();
List<SelectListItem> itemsSelect =new List<SelectListItem>;
sjfs00
0 Points
22 Posts
Re: How to save the selected value of dropdownlist in DB
May 07, 2010 07:58 AM|LINK
Hi, good morning !
I adjusted the code as you said, and know nothing is underlined in the controller, accohererding to the intellisense, it's perfect.
But i think there is something missing, you didn't use the Viewdata to grap information, and then send it to the dropdownlist.
And when i hit F5, there is an error when trying to use the action "Create" , it says something about Viewdata:
Thanks a lot for your interest Sir !!