Francesco Bigi

Francesco Bigi

  • 1.7k
  • 57
  • 82.1k

DropDownListFor unable to set on string (MVC)

Jan 26 2017 5:41 AM
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:
 

On my Folder Account -> AccountViewModels.cs I put these codes:
  1. public class RegisterViewModel    
  2.     {    
  3.         [Required]    
  4.         [Display(Name = "Etat civil")]    
  5.         [Range(1, 2, ErrorMessage = "Veuillez choisir")]    
  6.         public string Civilite { getset;     
  7.      }    
  8. public class EtatCivil    
  9.     {    
  10.         public int Id { getset; }    
  11.         public string Libelle { getset; }    
  12.     }  
and on Controllers -> AccountController.cs
  1. //  
  2.         // GET: /Account/Register  
  3.         [AllowAnonymous]  
  4.         public ActionResult Register()  
  5.         {  
  6.             var lstEtat = new List<EtatCivil>  
  7.             {  
  8.                 new EtatCivil {Id = 0, Libelle = "vide"},  
  9.                 new EtatCivil {Id = 1, Libelle = "Célibataire"},  
  10.                 new EtatCivil {Id = 2, Libelle = "Marié"}  
  11.             };  
  12.             ViewBag.ListEtatCivil = new SelectList(lstEtat, "Id""Libelle");  
  13.             return View();  
  14.         }  
 
 
  1. //  
  2.         // POST: /Account/Register  
  3.         [HttpPost]  
  4.         [AllowAnonymous]  
  5.         [ValidateAntiForgeryToken]  
  6.         public async Task<ActionResult> Register(RegisterViewModel model)  
  7.         {  
  8.             if (!ModelState.IsValid) return View(model);  
  9.             var user = new ApplicationUser() { UserName = model.UserName };  
  10.             var result = await UserManager.CreateAsync(user, model.Password);  
  11.             if (result.Succeeded)  
  12.             {  
  13.                 try {  
  14.                     // ajout du user dans la table client  
  15.                     using (var dal = new DALEF.WebShopEntities())  
  16.                     {  
  17.                         dal.Client.Add(  
  18.                             new DALEF.Client  
  19.                             {  
  20.                                 CLI_Nom = model.Nom,  
  21.                                 CLI_Prenom = model.Prenom,  
  22.                                 CLI_Civilite = model.Civilite,  
  23.                                 CLI_Email = model.Email,  
  24.                                 CLI_Adresse = model.Adresse,  
  25.                                 CLI_CodePostal = model.CodePostal,  
  26.                                 CLI_Ville = model.Ville,  
  27.                                 CLI_Telephone = model.Telephone,  
  28.                                 CLI_AspUser_Id = user.Id  
  29.                             });  
  30.                         dal.SaveChanges();  
  31.                     }  
  32.   
  33.                 }  
  34.                 catch(Exception) {                         
  35.                     //await UserManager.DeleteAsync(user);  
  36.                     ModelState.AddModelError("""Echec création client, veuillez réessayer");  
  37.                     return View(model);  
  38.                 }  
  39.                 // on affecte le rôle client  
  40.                 UserManager.AddToRole(user.Id, "client");  
  41.                 await SignInAsync(user, isPersistent: false);  
  42.                 return RedirectToAction("Index""Home");  
  43.             }  
  44.             var lstEtat = new List<EtatCivil>  
  45.             {  
  46.                 new EtatCivil {Id = 0, Libelle = "vide"},  
  47.                 new EtatCivil {Id = 1, Libelle = "Célibataire"},  
  48.                 new EtatCivil {Id = 2, Libelle = "Marié"}  
  49.             };  
  50.             ViewBag.ListEtatCivil = new SelectList(lstEtat, "Id""Libelle");  
  51.   
  52.             AddErrors(result);  
  53.   
  54.             // If we got this far, something failed, redisplay form  
  55.             return View(model);  
  56.         }  
 On my  Views --> Register.cshtml
 
  1. <div class="form-group">  
  2.         @Html.LabelFor(m => m.Civilite, new { @class = "col-md-2 control-label" })  
  3.         <div class="col-md-10">  
  4.             @Html.DropDownListFor(m => m.Civilite, (SelectList)ViewBag.ListEtatCivil)  
  5.   
  6.         </div>  

What am I missing? 
 
 

Answers (2)