简体   繁体   中英

How to GET two models in one web api controller?

So I have two tables in my db, engineer and engineerSettings. I currently have a controller for both api/username and api/userSettings, but I also want to have a controller api/userInformation that returns everything in username as well as userSettings. I'm not really sure how to go about doing this, but right now I have a new model for userInformation

public class userInformation
{
    public virtual ICollection<Username> Usernames {get;set;}
    punlic virtual ICollection<UserSetting> UserSettings {get;set;}
}

And in my UserInformation controller I have

.... 
//GET
public IEnumerable<UserInformation> GetUserInformation()
{
    return db.UserInformations;
}

Which doesn't really work so I was wondering how to solve this problem?

Fill your model:

public UserInformation GetUserInformation()
{
        UserInformation UserInfo= new UserInformation();
        List<Username> lstUsername= new List<Username>();
        List<UserSetting> lstUserSetting= new List<UserSetting>();
        lstUserSetting=db.UserSettings.ToList();
        lstUsername=db.Usernames.ToList();
        UserInfo.Usernames =lstUsername;
        UserInfo.UserSettings =lstUserSetting;
        return UserInfo;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM