c# - cannot insert dropdownlist value to database in mvc -


i have registerviewmodel takes input user , store in database. have used ienumerable storing roles in dropdownlist.the view works fine unable dropdownlist value , store user table. should dropdownlist value?

public class registerviewmodel     {         [required]         [displayname("user name")]         public string username { get; set; }         [required]         [remote("emailalreadyexists", "validation", errormessage = "user mail exists")]         [displayname("email")]         public string email { get; set; }         [required]         [displayname("password")]         public string password { get; set; }         [required]         [datatype(datatype.password)]         [displayname("confirm password")]         public string confirmpassword { get; set; }         [required]         public ienumerable<selectlistitem> roles { get; set; }     } 

my role model

public class role {     public int id { get; set; }     public string  name { get; set; } }         

view roles is,

<div class="form-group">     @html.labelfor(model => model.roles, htmlattributes: new { @class = "control-label col-md-2" })     <div class="col-md-10">         @html.dropdownlistfor(model => model.roles, model.roles, "select roles" , new {id = "roles" })         @html.validationmessagefor(model => model.roles, "", new { @class = "text-danger" })     </div> </div> 

and controller

public actionresult index() {     registerviewmodel uvm = new registerviewmodel();     uvm.roles = r in roles     select new system.web.mvc.selectlistitem     {         text = r.name,         value = r.id.tostring()     };     return view(uvm);                  }  [httppost] [validateantiforgerytoken] public actionresult index(registerviewmodel register) {     if (modelstate.isvalid)     {         string code = guid.newguid().tostring();          userrepo.insert(new models.user()         {             username = register.username,             email = register.email,             password = register.password,                 /* role = register.roles */                           createddate = datetime.now,             authcode = code,             status = false         });           sendmail(register.username, register.email, code);         return redirecttoaction("thankyou");     }      return view(register); } 

add new property in registerviewmodel

 public class registerviewmodel  {     [required]                                     [displayname("user name")]                                     public string username { get; set; }                                     [required]                                     [remote("emailalreadyexists", "validation", errormessage = "user mail exists")]                                     [displayname("email")]                                     public string email { get; set; }                                     [required]                                     [displayname("password")]                                     public string password { get; set; }                                     [required]                                     [datatype(datatype.password)]                                     [displayname("confirm password")]                                     public string confirmpassword { get; set; }                                     [required]                                     public ienumerable<selectlistitem> roles { get; set; }    **public string selectedrole {get;set;}**  } 

and dropdown sould bewlo

@html.dropdownlistfor(model => model.selectedrole , model.roles, "select roles" , new {id = "roles" }) 

you controller action like

[httppost]                             [validateantiforgerytoken]                             public actionresult index(registerviewmodel register)                             {                                 if (modelstate.isvalid)                                 {                                     string code = guid.newguid().tostring();                                      userrepo.insert(new models.user()                                     {                                         username = register.username,                                         email = register.email,                                         password = register.password,                                             role = register.selectedrole                                         createddate = datetime.now,                                         authcode = code,                                         status = false                                     });                                       sendmail(register.username, register.email, code);                                     return redirecttoaction("thankyou");                                 }                                  return view(register);                             } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -