Click here to Skip to main content
13,627,615 members
Rate this:
 
Please Sign up or sign in to vote.
See more: , +
Hello People,
I have this issue:
I want to put the marital status on drop down list, when I make the register of the new user, and chose married or unmarried, and click on the save button, on my db i see that it received the id (1, 2) instead of "married" or "unmarried".
This is what I see on my db:
See image


What am I missing? 


What I have tried:

On my Folder Account -> AccountViewModels.cs I put these codes:
public class RegisterViewModel    
    {    
        [Required]    
        [Display(Name = "Etat civil")]    
        [Range(1, 2, ErrorMessage = "Veuillez choisir")]    
        public string Civilite { get; set;     
     }    
public class EtatCivil    
    {    
        public int Id { get; set; }    
        public string Libelle { get; set; }    
    } 


and on Controllers -> AccountController.cs
//  
        // GET: /Account/Register  
        [AllowAnonymous]  
        public ActionResult Register()  
        {  
            var lstEtat = new List<EtatCivil>  
            {  
                new EtatCivil {Id = 0, Libelle = "vide"},  
                new EtatCivil {Id = 1, Libelle = "Célibataire"},  
                new EtatCivil {Id = 2, Libelle = "Marié"}  
            };  
            ViewBag.ListEtatCivil = new SelectList(lstEtat, "Id", "Libelle");  
            return View();  
        }  


//  
        // POST: /Account/Register  
        [HttpPost]  
        [AllowAnonymous]  
        [ValidateAntiForgeryToken]  
        public async Task<ActionResult> Register(RegisterViewModel model)  
        {  
            if (!ModelState.IsValid) return View(model);  
            var user = new ApplicationUser() { UserName = model.UserName };  
            var result = await UserManager.CreateAsync(user, model.Password);  
            if (result.Succeeded)  
            {  
                try {  
                    // ajout du user dans la table client  
                    using (var dal = new DALEF.WebShopEntities())  
                    {  
                        dal.Client.Add(  
                            new DALEF.Client  
                            {  
                                CLI_Nom = model.Nom,  
                                CLI_Prenom = model.Prenom,  
                                CLI_Civilite = model.Civilite,  
                                CLI_Email = model.Email,  
                                CLI_Adresse = model.Adresse,  
                                CLI_CodePostal = model.CodePostal,  
                                CLI_Ville = model.Ville,  
                                CLI_Telephone = model.Telephone,  
                                CLI_AspUser_Id = user.Id  
                            });  
                        dal.SaveChanges();  
                    }  
  
                }  
                catch(Exception) {                         
                    //await UserManager.DeleteAsync(user);  
                    ModelState.AddModelError("", "Echec création client, veuillez réessayer");  
                    return View(model);  
                }  
                // on affecte le rôle client  
                UserManager.AddToRole(user.Id, "client");  
                await SignInAsync(user, isPersistent: false);  
                return RedirectToAction("Index", "Home");  
            }  
            var lstEtat = new List<EtatCivil>  
            {  
                new EtatCivil {Id = 0, Libelle = "vide"},  
                new EtatCivil {Id = 1, Libelle = "Célibataire"},  
                new EtatCivil {Id = 2, Libelle = "Marié"}  
            };  
            ViewBag.ListEtatCivil = new SelectList(lstEtat, "Id", "Libelle");  
  
            AddErrors(result);  
  
            // If we got this far, something failed, redisplay form  
            return View(model);  
        }  


On my Views --> Register.cshtml

<div class="form-group">  
        @Html.LabelFor(m => m.Civilite, new { @class = "col-md-2 control-label" })  
        <div class="col-md-10">  
            @Html.DropDownListFor(m => m.Civilite, (SelectList)ViewBag.ListEtatCivil)  
  
        </div> 
Posted 25-Jan-17 23:53pm
Updated 27-Jan-17 0:36am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

That's how dropdowns work, there are two elements, the name and the value. The name is what appears in the list and the value is what is submitted with the form. Normally you'd convert the ID back to the relevant text when needed. However if you really do only want the text and you don't care about the ID then make both the value and name the text you want. However I suspect that's not what you really want to do.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Many thanks to F-ES Sitecore for your help!

Actually what I have done is this:
//
        // POST: /Account/Register
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            var lstEtat = new List<EtatCivil>
            {
                new EtatCivil {Id = 0, Libelle = "vide"},
                new EtatCivil {Id = 1, Libelle = "Célibataire"},
                new EtatCivil {Id = 2, Libelle = "Marié"}
            };
            ViewBag.ListEtatCivil = new SelectList(lstEtat, "Id", "Libelle");
            if (!ModelState.IsValid) return View(model);
            var user = new ApplicationUser() { UserName = model.UserName };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                try {
                    // ajout du user dans la table client
                    using (var dal = new DALEF.WebShopEntities())
                    {

                        dal.Client.Add(
                            new DALEF.Client
                            {
                                CLI_Nom = model.Nom,
                                CLI_Prenom = model.Prenom,
                                CLI_Civilite = lstEtat.Find(x=>x.Id == int.Parse(model.Civilite)).Libelle,
                                CLI_Email = model.Email,
                                CLI_Adresse = model.Adresse,
                                CLI_CodePostal = model.CodePostal,
                                CLI_Ville = model.Ville,
                                CLI_Telephone = model.Telephone,
                                CLI_AspUser_Id = user.Id
                            });
                        dal.SaveChanges();
                    }

                }
                catch(Exception) {                       
                    //await UserManager.DeleteAsync(user);
                    ModelState.AddModelError("", "Echec création client, veuillez réessayer");
                    return View(model);
                }
                // on affecte le rôle client
                UserManager.AddToRole(user.Id, "client");
                await SignInAsync(user, isPersistent: false);
                return RedirectToAction("Index", "Home");
            }

            

            AddErrors(result);

            // If we got this far, something failed, redisplay form
            return View(model);
        }


An it works, I can see on my db the status on string and not on int. :)
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180712.1 | Last Updated 27 Jan 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100