ASP.NET Core API Only Returning First Result of List

ASP.NET Core API only returning first result of list

Add this to Startup.cs inside the public void ConfigureServices(IServiceCollection services) method:

services.AddMvc().AddJsonOptions(options => {
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});

The issue was discussed https://github.com/aspnet/Mvc/issues/4160 and https://github.com/aspnet/EntityFramework/issues/4646 also see circular reference

Database query returning only the first element of the data base as the same instance for all, in ASP.NET MVC Core 6

It's better not to share image, but code using correct tag. To use code helps other users and all the cummunity, for example it makes your question searchable.

Anyway you can try to do in this way:

if (!string.IsNullOrEmpty(id)){
List<HealthDataFuture> DataList = _db.HealthDataFuture.Where(x => h.Id == id).ToList();
return View(id);
}
return RedirectToAction("Index");

I've also some question about your code:

  1. What should it do?
  2. DataList is like a select query from db, but nobody is using DataList. Why?

The more details you provide, the more information we have to help you.

Editing after comments:

If you want to remove data from db you should use saveChanges. For example, if you want to remove all lines with id other than "1", you can try:

if (!string.IsNullOrEmpty(id)){
_db.HealthDataFuture.RemoveRange(_db.HealthDataFuture.Where(x => h.Id != id));
_db.SaveChanges();
return View(id);
}
return RedirectToAction("Index");

You can also read something about access to DB in async way, it's recommended and it perfoms better: https://learn.microsoft.com/it-it/ef/core/miscellaneous/async

Only return selected fields in Web API results

It's because you're returning a collection of Publication objects so you will get every property that is contained in that class, whether you populate it or not. If you want to return a subset of the properties then create a class that has only the properties you want to return and create an instance of that class in your query.

public IQueryable<WhatIReallyWantToReturn> GetPublications()
{
return db.Publications
.ToList()
.Select(p => new WhatIReallyWantToReturn {
PublicationID = p.PublicationID,
PublicationTitle = p.PublicationTitle,
Frequency = p.Frequency,
NextIssueDate = p.NextIssueDate
})
.AsQueryable();
}

private class WhatIReallyWantToReturn
{
public int PublicationID { get; set; }
public string PublicationTitle { get; set; }
public string Frequency { get; set; }
public DateTime NextIssueDate { get; set; }
}

Get a list of Roles as a Liststring as result in ASP.NET Core Web API

You can make method async that way

public async Task<List<string>> GetRoles()
{
List<string> roles = await _roleManager.Roles.Select(x => x.Name).ToListAsync();

return roles;
}

Or you can just return List instead of Task

public List<string> GetRoles()
{
List<string> roles = _roleManager.Roles.Select(x => x.Name).ToList();

return roles;
}


Related Topics



Leave a reply



Submit