小编mdr*_*rap的帖子

WebAPI返回没有根节点的JSON数组

我在EmployeeController中有以下示例代码,它创建了几个员工,将它们添加到员工列表,然后在get请求中返回员工列表.代码中返回的JSON包括Employees作为根节点.我需要返回一个没有Employees属性的JSON数组,因为每当我尝试将JSON结果解析为对象时,我都会收到错误,除非我手动重新格式化字符串以不包含它.

public class Employee
{
    public int EmployeeID { get; set; }
    public string Name { get; set; }
    public string Position { get; set; }
}

public class EmployeeList
{
    public EmployeeList()
    {
        Employees = new List<Employee>();
    }
    public List<Employee> Employees { get; set; }
}


public class EmployeeController : ApiController
{
    public EmployeeList Get()
    {
        EmployeeList empList = new EmployeeList();
        Employee e1 = new Employee
        {
            EmployeeID = 1,
            Name = "John",
            Position = "CEO"
        };
        empList.Employees.Add(e1);
        Employee …
Run Code Online (Sandbox Code Playgroud)

c# json json.net asp.net-web-api2

6
推荐指数
1
解决办法
5330
查看次数

ASP.NET MVC 传递多个模型以从控制器查看

我需要一个视图来显示员工的名字和姓氏以及员工关联的主管名字和姓氏。

我有 2 个模型,它们如下:

public class Employee
{
    public int EmployeeID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Department { get; set; }
    public int SupervisorID { get; set; }

    public virtual ICollection<Supervisor> Supervisor { get; set; }
}

public class Supervisor
{
    public int SupervisorID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc entity-framework

2
推荐指数
1
解决办法
6109
查看次数