c# - Access UserProfile from a controller MVC 4 -
new this: have model character has userprofile property, character can assiciated userprofile entry. make userprofile foreign key character.
character model:
public class character { public int id { get; set; } public virtual userprofile user { get; set; } public string name { get; set; } ... }
(i made userprofile property virtual because of post, not sure if should stay)
when creating new character want set user property of character object of current web-user making request. example here process user take make character: login/create user account site click 'create character'
[httppost] public actionresult create(character character) { if (modelstate.isvalid) { character.user = ??? db.characters.add(character); db.savechanges(); ... }
problem not know how access userprofile table controller. typically access table create object of dbcontext userprofile table using default mvc 4 context while other objects of app using different dbcontext.
i'm guessing there few solutions me, none of can figure out how do:
- should set application tables use 1 dbcontext? if so, how?
- is there way create object of default dbcontext userprofile using?
sorry in advance if of terminology off or if left out vital information. i'll provide more needed. thanks
i save current user id in session variable , retrieve user db when need it. code like:
[httppost] public actionresult create(character character) { if (modelstate.isvalid) { character.user = db.userprofiles.find(u => u.userid = (int) session["userid"]); db.characters.add(character); db.savechanges(); ... }
Comments
Post a Comment