Home/ASP.NET Forums/General ASP.NET/MVC/How to save the selected value of dropdownlist in DB

How to save the selected value of dropdownlist in DB [Answered]RSS

36 replies

Last post May 12, 2010 05:40 PM by ignatandrei

My .NET blog : http://msprogrammer.serviciipeweb.ro/ |2012 Programmer tools
  • sjfs00

    sjfs00

    Member

    0 Points

    22 Posts

    Re: How to save the selected value of dropdownlist in DB

    May 08, 2010 06:37 PM|sjfs00|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
  • sjfs00

    sjfs00

    Member

    0 Points

    22 Posts

    Re: How to save the selected value of dropdownlist in DB

    May 08, 2010 06:50 PM|sjfs00|LINK

    for help : i read in a forum that they put this in the post :

    var articleTypeId = Request.Form["ArticleTypeID"];
      var categoryId = Request["CategoryID"];



    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 !

    sjfs00
  • ignatandrei

    ignatandrei

    All-Star

    153689 Points

    25264 Posts

    Moderator

    MVP

    Re: How to save the selected value of dropdownlist in DB

    May 08, 2010 06:57 PM|ignatandrei|LINK

    sjfs00

    but the :

    "

    ModelState.AddModelError("", Ex.message);

    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

    sjfs00

    when i click on "Creat" button, it relods the page with the data i entred, but , nothing happens to the DB.  

    My .NET blog : http://msprogrammer.serviciipeweb.ro/ |2012 Programmer tools
  • sjfs00

    sjfs00

    Member

    0 Points

    22 Posts

    Re: How to save the selected value of dropdownlist in DB

    May 08, 2010 07:14 PM|sjfs00|LINK

    ah , okay , i corrected the catch syntax :

    catch  (Exception Ex)
                {
                  }

    BUT 




    ? what to do Now Sir ?


    sjfs00
  • ignatandrei

    ignatandrei

    All-Star

    153689 Points

    25264 Posts

    Moderator

    MVP

    Re: How to save the selected value of dropdownlist in DB

    May 08, 2010 07:22 PM|ignatandrei|LINK

    This is ORM problem. You want to create T_Pointage_Materiel without creating T_Imputation.

    Does the T_Pointage_Materiel NEED an T_Imputation or not  ? ( as a business requirement )

    My .NET blog : http://msprogrammer.serviciipeweb.ro/ |2012 Programmer tools
  • sjfs00

    sjfs00

    Member

    0 Points

    22 Posts

    Re: How to save the selected value of dropdownlist in DB

    May 09, 2010 12:38 AM|sjfs00|LINK

    y the

    euh , i dont see what you mean clearly, but i can explain what i want to do.

     

    First the T_Imputation already exists, so we can get a list of "T_imputation" in the dropdownlist.

    in "T_pointageMateriel" , we have many fiels or columns, + 2 other columns, but they are foreign Keys, these columns are "Materiel" and "imputation" and "Materiel"

    So when i hit "Creat" button, i d like to save  the selected value in the "T_pointageMateriel". this action doesn't have anything to do with "T_imputation", excepte the loading ( precisely in the Ation GET, ) to fill the 2 dropdownlists.

    the active table here , which gana be used to save is the "T_PointageMateriel" only.



    thanks a lot ! :)


    sjfs00
  • ignatandrei

    ignatandrei

    All-Star

    153689 Points

    25264 Posts

    Moderator

    MVP

    Re: How to save the selected value of dropdownlist in DB

    May 09, 2010 04:54 AM|ignatandrei|LINK

    OK, now I see...

    That depends if the ORM that you are using(EF, L2S, NHibernate, other) generates the Foreign Key T_Imputation for a T_pointageMateriel or not.

    EF3.5 and L2S does not create, EF3 yes.

    What are you need to do is re-query the database for the value in dropdownlist T_Imputation, find a T_Imputation object and attach to T_pointageMateriel

    For your situation you can do the following :

    In the Create add a new parameter , long imputations and query the database for a T_inmputation and attach to T_pointageMateriel

    Please see (self advertising) http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/ - it is the same . See the create function and the SaveNew function.



    My .NET blog : http://msprogrammer.serviciipeweb.ro/ |2012 Programmer tools
  • rezaxp

    rezaxp

    Participant

    1971 Points

    386 Posts

    Re: How to save the selected value of dropdownlist in DB

    May 09, 2010 05:40 AM|rezaxp|LINK

    hi ignatandrei

    you are really greate in answering questions.you r one of my targets for be an answerer.

    but about this thread, did you solve your problem?

    Please don't forget Mark as Answered if you got your answer

    Life would be so much easier if we could just look at the source code.
  • sjfs00

    sjfs00

    Member

    0 Points

    22 Posts

    Re: How to save the selected value of dropdownlist in DB

    May 09, 2010 11:25 AM|sjfs00|LINK

    Hi Mr Ignatandrei !

    thanks for the answer.

    for the technolog am using in this project is : MVC 1.0 and the ADO.Net Entity framework, i  dont know which version it is, i use it in Visual studio 2008 Prof Edition.

    i read the article in the blog, but it's not compatible, i have the same problem, for about one week now.

    it is : the "T_pointage materiel" does not have the  " .id_imputation" as a property ! and i dont see how to do so the intellisence would recognize this ".id_imputation" . 

    i want to precise that in the model.edmx, we see clearly tha there is a relation between the "T_pointageMateriel" and the "t_imputation"

    here is a snapshot of the entity diagram designer :

    http://img534.imageshack.us/img534/5863/entitydesignerdiagram.png





    the :

    (re-query the database for the value in dropdownlist T_Imputation, find a T_Imputation object and attach to T_pointageMateriel)

    <div>seems to be the solution, i d like to do this operation but i cant go ahead when the .id_imputation doesnt work ! </div><div>What can i do for this Sir ?</div><div>
    </div><div>
    </div><div>Kind Regards !</div>
    sjfs00
  • ignatandrei

    ignatandrei

    All-Star

    153689 Points

    25264 Posts

    Moderator

    MVP

    Re: How to save the selected value of dropdownlist in DB

    May 09, 2010 07:40 PM|ignatandrei|LINK

    sjfs00

    for the technolog am using in this project is : MVC 1.0 and the ADO.Net Entity framework, i  dont know which version it is, i use it in Visual studio 2008 Prof Edition.

    i read the article in the blog, but it's not compatible, i have the same problem, for about one week now.

    it is : the "T_pointage materiel" does not have the  " .id_imputation" as a property ! and i dont see how to do so the intellisence would recognize this ".id_imputation" . 

    Oh, yes. You have the same problem.(although I use MVC 2 in the project this is EF 3.5 problem)

    Please modify :

    1. public ActionResult Create( T_Pointage_Materiel PMACreer, long imputations /* the name of the dropdown*/)  
    2.         { 
    3. UpdateModel(PMACreer);
    4. PMACreer.T_Imputation = _db.T_Imputations.Where(x=>x.<the name of the Primary key> == imputations).First();
    5.                 _db.AddToT_Pointage_Materiel(PMACreer);  
    6.                 _db.SaveChanges();  
    7.                 return RedirectToAction("Index"); 
    My .NET blog : http://msprogrammer.serviciipeweb.ro/ |2012 Programmer tools
  • ignatandrei

    ignatandrei

    All-Star

    153689 Points

    25264 Posts

    Moderator

    MVP

    Re: How to save the selected value of dropdownlist in DB

    May 09, 2010 07:45 PM|ignatandrei|LINK

    rezaxp

    but about this thread, did you solve your problem?

    There is not my problemCool - and I think that 90% of the answers are on my blog post

    rezaxp

    you are really greate in answering questions.you r one of my targets for be an answerer.

    thank you for kind words

    My .NET blog : http://msprogrammer.serviciipeweb.ro/ |2012 Programmer tools
  • sjfs00

    sjfs00

    Member

    0 Points

    22 Posts

    Re: How to save the selected value of dropdownlist in DB

    May 10, 2010 08:46 AM|sjfs00|LINK


    Good Morning Sir !


    I tried what you did, but i stilll have the error : " There is no ViewData item with the key 'Imputation' of type 'IEnumerable<SelectListItem>'. "


    here isthe code :

    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["Imputation"] = 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, string Materiel, long Imputation)
            {
                try
                {
    
    
                    
    
    
    
                    UpdateModel(PMACreer);
                    PMACreer.T_Materiel = _db.T_Materiel.Where(x => x.Num_anael == Materiel.ToString()).First();
                    PMACreer.T_imputation = _db.T_imputation.Where(x=>x.id_imputation == Imputation).First();
                    
    
    
    
                    _db.AddToT_Pointage_Materiel(PMACreer);
                    _db.SaveChanges();
                    return RedirectToAction("Index");
                }
                catch  (Exception Ex)
                {
                    //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();
                }
            }


    ------------------------------------------

    i have some questions,

    * If the problem is on the EF3.5, can upgrade it to Version 4 , but with visual 2008 ?

    * Can i completely change the framework, and use the L2S or anything else with less problems ?

    * Can I use WEbforms for the fragmentsof my project that require dropdownlists, i mean can i use WebForm in an MVC context ?

    * And finally, does it bother you if i give you an archive with DB + Project Solution Sln, to see if you can do something to thispersistent issue ?


    Thanks Sir,

    Hopefully to find a solution for this !


    sjfs00
  • imran_ku07

    imran_ku07

    All-Star

    48256 Points

    8137 Posts

    MVP

    Re: How to save the selected value of dropdownlist in DB

    May 10, 2010 09:12 AM|imran_ku07|LINK

    %3C (<) %3E (>) is allowed in URL PAth

    http://www.asp.net/LEARN/whitepapers/aspnet4#0.2__Toc253429244

    "And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
    Excellent Windows VPS Hosting
    Imran Baloch MVP, MVB, MCP, MCTS, MCPD
  • ignatandrei

    ignatandrei

    All-Star

    153689 Points

    25264 Posts

    Moderator

    MVP

    Re: How to save the selected value of dropdownlist in DB

    May 10, 2010 09:16 AM|ignatandrei|LINK

    practically in catch you must redo the same as in create to fill the dropdown :

    catch  (Exception Ex)  
                {  
                    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["Imputation"] = 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();  
                }

    My .NET blog : http://msprogrammer.serviciipeweb.ro/ |2012 Programmer tools
  • ignatandrei

    ignatandrei

    All-Star

    153689 Points

    25264 Posts

    Moderator

    MVP

    Re: How to save the selected value of dropdownlist in DB

    May 10, 2010 09:18 AM|ignatandrei|LINK

    imran_ku07

    %3C (<) %3E (>) is allowed in URL PAth

    http://www.asp.net/LEARN/whitepapers/aspnet4#0.2__Toc253429244

    is allowed, but in his case is not a good action/controller  Cool

    My .NET blog : http://msprogrammer.serviciipeweb.ro/ |2012 Programmer tools
  • imran_ku07

    imran_ku07

    All-Star

    48256 Points

    8137 Posts

    MVP

    Re: How to save the selected value of dropdownlist in DB

    May 10, 2010 09:23 AM|imran_ku07|LINK

    ignatandrei

    is allowed, but in his case is not a good action/controller  Cool

    Not undersatnd what do you mean?

    "And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
    Excellent Windows VPS Hosting
    Imran Baloch MVP, MVB, MCP, MCTS, MCPD
  • ignatandrei

    ignatandrei

    All-Star

    153689 Points

    25264 Posts

    Moderator

    MVP

    Re: How to save the selected value of dropdownlist in DB

    May 10, 2010 10:30 AM|ignatandrei|LINK

    imran_ku07

    ignatandrei

    is allowed, but in his case is not a good action/controller  Cool

    Not undersatnd what do you mean?

    He had

    <div>using (Html.BeginForm("<create>""<PointageMateriel>"))</div><div>I suggest him put</div><div><div>using (Html.BeginForm("create""PointageMateriel"))</div></div>
    My .NET blog : http://msprogrammer.serviciipeweb.ro/ |2012 Programmer tools
  • imran_ku07

    imran_ku07

    All-Star

    48256 Points

    8137 Posts

    MVP

    Re: How to save the selected value of dropdownlist in DB

    May 10, 2010 11:38 AM|imran_ku07|LINK

    ignatandrei

    <div>using (Html.BeginForm("<create>""<PointageMateriel>"))</div><div>I suggest him put</div><div><div>using (Html.BeginForm("create""PointageMateriel"))</div></div>

    This is why Bad Request Exception comes into action

    "And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
    Excellent Windows VPS Hosting
    Imran Baloch MVP, MVB, MCP, MCTS, MCPD
  • sjfs00

    sjfs00

    Member

    0 Points

    22 Posts

    Re: How to save the selected value of dropdownlist in DB

    May 12, 2010 02:37 PM|sjfs00|LINK

    Hi everybody !


    this problem is still unresolved.

    I made a change in my project and tried to use FORMS, it was quiet simple in front of this asp.net Mvc & EF3.5, using the wizard i did something good, i could retrieve data from the DB and display them in a DropDL, and save when clicking the "Create". BUT !

    but i couldnt hide the difference when moving from the MVC to pages that uses FORMS. (problem wth inheritence from the Master.page ...)


    Then i decided to drop this ###  entity framework 3.5 Yell , described as (powerfull , new and well developped) , and i used the L2S.

    and here i found my paradise , it worked. i had to change code, it was simplier.


    i regret, all the time i spent on this EF3.5 !


    Now i have to move on , and start the second part of the project which is the reporting services.


    as advice : USE L2S rather than EF3.5 



    Thanks for all for the replies, and a special thanks to "ignatandrei"

    Good Luck Folks ! Cool

    sjfs00
  • ignatandrei

    ignatandrei

    All-Star

    153689 Points

    25264 Posts

    Moderator

    MVP

    Re: How to save the selected value of dropdownlist in DB

    May 12, 2010 05:40 PM|ignatandrei|LINK

    sjfs00

    as advice : USE L2S rather than EF3.5 

    You are perfectly right ... however, EF4 it's the new kid - and it becomes slightly better than L2S. More, L2S will be developed "on demand" - so I suggest keep the EF as a tool

    sjfs00

    and a special thanks to "ignatandrei"

    Thank you for the kind words!


    My .NET blog : http://msprogrammer.serviciipeweb.ro/ |2012 Programmer tools
  • ‹ Previous Thread|Next Thread ›