简体   繁体   English

如何在一个Web API控制器中获取两个模型?

[英]How to GET two models in one web api controller?

So I have two tables in my db, engineer and engineerSettings. 所以我的数据库中有两个表,engineer和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. 我目前有一个同时用于api / username和api / userSettings的控制器,但我也想拥有一个返回用户名和userSettings中所有内容的控制器api / userInformation。 I'm not really sure how to go about doing this, but right now I have a new model for userInformation 我不确定如何执行此操作,但是现在我有了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 在我的UserInformation控制器中

.... 
//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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一个Web API控制器来控制所有模型 - One Web API Controller to control all models 在Web Api中,如何在不使用属性的情况下使用两个get方法指定到一个控制器的两条单独的路由? - In Web Api, how can I specify two separate routes to one controller with two get methods without using attributes? 如何在一个控制器/JOIN 中从我的数据库返回/获取两个模型/表或使用存储过程 - How to return / GET two models/tables from my DB in one controller / JOIN or use stored procedure 两个HTTP Post方法在一个Web API控制器中具有不同的参数 - Two HTTP Post method with different parameters in one Web API controller 在一个控制器中比较来自两个模型的数据 - Comparing data from two models in one controller 如何使用MVC4 Web API控制器调用两个以上的Get方法 - How to give a call to more than two Get method using mvc4 web api controller 在Odata Web API控制器中定义两个get函数 - defining two get functions in Odata web api controller 如何使用局部视图在一页上获得两种不同的模型? - How to get two different models on one page with Partial Views? 如何在一个控制器中的Web API中发布多个对象数据json - How to post multiple object data json in web api in one controller 如何获得具有一对多关系的Web API - How to get Web API with one to many relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM