I have Asp.Net core 3.1 Web Api where a complex object sould be returned by an action like a Json, the problem is that the returned object does not contain the list of child objects:
Below the object:
public class DepartementViewModel
{
public int Dep_ID { get; set; }
public string Dep_Name { get; set; }
public List<BasicEmpViewModel> listEmployees = new List<BasicEmpViewModel>();
}
And the Action
[HttpGet]
public async Task<ActionResult<IEnumerable<DepartementViewModel>>> GetDepartement()
{
IRepository IRepos = new DepartementRepository(_context);
IList<DepartementViewModel> ilIst = await IRepos.GetList();
return Ok(ilIst);
}
The repository GetList function
public async Task<IList<DepartementViewModel>> GetList()
{
IList<DepartementViewModel> listDept = new List<DepartementViewModel>();
listDept = await(from dept in _context.Departement
orderby dept.Dep_ID ascending
select new DepartementViewModel
{
dept.Dep_ID ,
dept.Dep_Name
}
).ToListAsync();
listDept.ForEach(x =>
{
var emObj =_context.Employee;
foreach (Employee E in emObj)
{
E.listEmployees.Add(new BasicEmpViewModel()
{
Emp_ID = E.Emp_ID,
Emp_Name = E.Emp_Name,
Checked = (E.Dep_ID == x.Dep_ID) ? true : false
}
);
}
});
return listDept;
}
The returned Json object does not contain the list of employees "listEmployees", it just displays information relating to the main object :Dep_ID and Dep_Name.
Is there something missing in my code ?.
Thank you
I suppose perhaps you are using System.Text.Json
library to serialize and deserialize data in the .net core application, right? If that is the case, perhaps the issue is related this library. As far as I know, when we using the System.Text.Json
library to serialize complex object, it will only return the outer object (without the inner entities).
To solve this issue, you could try to use the Microsoft.AspNetCore.Mvc.NewtonsoftJson
library to serialize data. Refer the following steps:
Install Microsoft.AspNetCore.Mvc.NewtonsoftJson
package via Nuget.
Register the NewtonsoftJson
in the Startup.ConfigureServices method:
services.AddControllersWithViews().AddNewtonsoftJson();
Here are some related articles about the System.Text.Json
and Microsoft.AspNetCore.Mvc.NewtonsoftJson
, you could refer them:
How to serialize and deserialize (marshal and unmarshal) JSON in .NET
I have found the solution, I publish the solution for any such problem. indeed it is necessary to put a Getter and Setter for the property listEmployees in the class DepartementViewModel like below
public class DepartementViewModel
{
public int Dep_ID { get; set; }
public string Dep_Name { get; set; }
public List<BasicEmpViewModel> listEmployees {get; set; };
}
Cordially
Took some digging to find, but your issue is that listEmployees
is a field . Currently, System.Text.Json
only supports the serialization of properties . You'll need to either change listEmployees
to a property or use Newtonsoft.Json
.
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.