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:

  1. should set application tables use 1 dbcontext? if so, how?
  2. 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

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -