c# - How to create custom additional fields in UserProfile -
i'm trying create custom user fields. i've added registration form. works properly, automatically creates table field city null. know why happens?
public class applicationuser : identityuser { public string city { get; set; } } public class applicationdbcontext : identitydbcontext<applicationuser> { static applicationdbcontext() { database.setinitializer(new mysqlinitializer()); } public applicationdbcontext() : base("mysql.data.mysqlclient") { } } mysqlinitializer.cs
public class mysqlinitializer : idatabaseinitializer<applicationdbcontext> { public void initializedatabase(applicationdbcontext context) { if (!context.database.exists()) { context.database.create(); } else { var migrationhistorytableexists = ((iobjectcontextadapter)context).objectcontext.executestorequery<int>( string.format( "select count(*) information_schema.tables table_schema = '{0}' , table_name = '__migrationhistory'", "[users]")); if (migrationhistorytableexists.firstordefault() == 0) { context.database.delete(); context.database.create(); } } } } register.cshtml
<div class="form-group"> @html.labelfor(m => m.city, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @html.textboxfor(m => m.city, new { @class = "form-control" }) </div> </div> accountviewmodels.cs in public class registerviewmodel added
[required] [display(name = "city")] public string city { get; set; } accountcontroller.cs
[httppost] [allowanonymous] [validateantiforgerytoken] public async task<actionresult> register(registerviewmodel model) { if (modelstate.isvalid) { var user = new applicationuser() { username = model.username }; var result = await usermanager.createasync(user, model.password); if (result.succeeded) { await signinasync(user, ispersistent: false); homecoming redirecttoaction("index", "home"); } else { adderrors(result); } } // if got far, failed, redisplay form homecoming view(model); }
you need include city variable when create user object:
[httppost] [allowanonymous] [validateantiforgerytoken] public async task<actionresult> register(registerviewmodel model) { if (modelstate.isvalid) { var user = new applicationuser() { username = model.username, city = model.city }; var result = await usermanager.createasync(user, model.password); if (result.succeeded) { await signinasync(user, ispersistent: false); homecoming redirecttoaction("index", "home"); } else { adderrors(result); } } // if got far, failed, redisplay form homecoming view(model); } edit: updated code utilize code.
c# asp.net-mvc asp.net-mvc-4 asp.net-membership asp.net-identity
No comments:
Post a Comment