Answered by:
Send data from multi tables in Web API

Question
-
User1690434716 posted
I have 3 tables : Employee,Department,Course.
public class Employee
{
public int EmpId {set;get;}
public string EmpName{set;get;}
}
public class Department
{
public int DepId {set;get;}
public string DepName{set;get;}
}
public class Course
{
public int CourseId {set;get;}
public string CourseName{set;get;}
}
I want to take data from them and send to Client . In Client, data is showed as 'dropdownlist'.
Thank you.
Monday, May 9, 2016 8:04 AM
Answers
-
User-369506445 posted
hi
create another class called ApiResult below like :
public class ApiResult { public List<Employee> Employees { get; set; } public List<Department> Departments { get; set; } public List<Course> Courses { get; set; } } public class Employee { public int EmpId { set; get; } public string EmpName { set; get; } } public class Department { public int DepId { set; get; } public string DepName { set; get; } } public class Course { public int CourseId { set; get; } public string CourseName { set; get; } }
and create a api controller called Univercity below like :
public class UnivercityController : ApiController { // GET: Univercity public IEnumerable<ApiResult> Get() { return new List<ApiResult>() { new ApiResult() { Courses = new List<Course>() { new Course() {CourseId = 1, CourseName = "CourseName1"}, new Course() {CourseId = 2, CourseName = "CourseName2"} }, Departments = new List<Department>() { new Department(){DepId = 1 , DepName = "DepName1"}, new Department(){DepId = 2 , DepName = "DepName2"} }, Employees = new List<Employee>() { new Employee(){ EmpId = 1 , EmpName = "EmpName" }, new Employee(){ EmpId = 1 , EmpName = "EmpName" } } } }; } }
now run your project and enter below url :
http://localhost:56446/api/Univercity
i hope it can be helpful
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 9, 2016 9:30 AM
All replies
-
User-369506445 posted
hi
create another class called ApiResult below like :
public class ApiResult { public List<Employee> Employees { get; set; } public List<Department> Departments { get; set; } public List<Course> Courses { get; set; } } public class Employee { public int EmpId { set; get; } public string EmpName { set; get; } } public class Department { public int DepId { set; get; } public string DepName { set; get; } } public class Course { public int CourseId { set; get; } public string CourseName { set; get; } }
and create a api controller called Univercity below like :
public class UnivercityController : ApiController { // GET: Univercity public IEnumerable<ApiResult> Get() { return new List<ApiResult>() { new ApiResult() { Courses = new List<Course>() { new Course() {CourseId = 1, CourseName = "CourseName1"}, new Course() {CourseId = 2, CourseName = "CourseName2"} }, Departments = new List<Department>() { new Department(){DepId = 1 , DepName = "DepName1"}, new Department(){DepId = 2 , DepName = "DepName2"} }, Employees = new List<Employee>() { new Employee(){ EmpId = 1 , EmpName = "EmpName" }, new Employee(){ EmpId = 1 , EmpName = "EmpName" } } } }; } }
now run your project and enter below url :
http://localhost:56446/api/Univercity
i hope it can be helpful
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, May 9, 2016 9:30 AM -
User1690434716 posted
I got it . Thank Vahid.
Monday, May 9, 2016 9:49 AM -
User-369506445 posted
i'm glad about it , if solve your problem please Mark As Answer it
thanks
Monday, May 9, 2016 10:28 AM -
User36583972 posted
Hi Ken.N,
The following tutorials may be helpful for you.
Add Models and Controllers:
http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-2
Best Regards,
Yohann Lu
Tuesday, May 10, 2016 1:47 AM