c# - Posting updated Address to the database MVC 5 -


i'm still learning how use mvc 5, , far manage custom fields user profiles seen here in manage view page:

http://puu.sh/ddmvy/2533472010.png

the user registers , fills out these fields , stored in same place username , password data stored. added these fields right under applicationuser in identitymodels.cs seen here

 public class applicationuser : identityuser     {          // additional user fields needed registration         public string firstname { get; set; }         public string lastname { get; set; }         public string address { get; set; }         public string city { get; set; }         public string state { get; set; }         public int zipcode { get; set; }           public async task<claimsidentity> generateuseridentityasync(usermanager<applicationuser> manager)         {             // note authenticationtype must match 1 defined in cookieauthenticationoptions.authenticationtype             var useridentity = await manager.createidentityasync(this, defaultauthenticationtypes.applicationcookie);             // add custom user claims here             return useridentity;         }     } 

i want able edit address if moves can update on own. have method data , put them inside textboxes in changeaddress view, when comes time update data i’m not sure how go that. i'm not sure how write post method here page looks below , hoping add in method in managecontroller.cs. i've seen other tutorials have done on separate table not 1 applicationuser.

http://puu.sh/ddn98/96cab8a252.png

my method display data in changeaddress view page

// get: /manage/changeaddress         [httpget]         public async task<actionresult> changeaddress()         {               applicationuser user = await usermanager.findbyidasync(user.identity.getuserid());              var model = new changeaddressviewmodel             {                 address = user.address,                 city = user.city,                 state = user.state,                 zipcode = user.zipcode              };             return view(model);         } 

you need pass in model allow identify user data belongs when comes back. easiest add userid property changeaddressviewmodel.

you pass other data in changeaddressviewmodel record when make request get. information populates view typically in form can submitted via post. typically put userid hiddenfield present in form not shown.

you can create update method on controller has [httppost] attribute takes changeaddressviewmodel input parameter , wire form submit posts update action.

inside new update method. locate desired user using userid have been passed back. set various updated values of user values obtained changeaddressviewmodel passed in post.

on db context, user record call savecnanges() update record in via ef.

there step step tutorial mvc on asp.net


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -