.NET Core API view model return

mo_maat

I am trying to run a query that returns only select fields off of my model and related entities. I get an error:

    Severity    Code    Description Project File    Line    Suppression State
    Error   CS0029  Cannot implicitly convert type
 'System.Collections.Generic.List<<anonymous type: int TeamId, string Name>>' to 'Microsoft.AspNetCore.Mvc.ActionResult<System.Collections.Generic.IEnumerable<ApplicationCore.Entities.TeamViewModel>>'    AppName C:....\Controllers\TeamController.cs    64  Active

What am I doing wrong?

  [HttpGet("{id}")]
    public async Task<ActionResult<IEnumerable<TeamViewModel>>> List(int id)
    {
        var team = await _context.Teams
            .Where(c => c.TeamId == id)
            .Select(c => new
            {
                c.TeamId,
                c.Team.Name
            })
            .ToListAsync();

        if (team == null)
        {
            return NotFound();
        }

        return team;
    }


class TeamViewModel
{
    [Required]
    public int TeamId { get; set; }
    [Required]
    public string TeamName { get; set; }
}
Nkosi

You are selecting an anonymous type and trying to return it as a concrete TeamViewModel type.

Assuming TeamViewModel was the intended type and that it is defined with the properties attempted in the original question, the action should be refactored..

[HttpGet("{id:int}")]
public async Task<ActionResult<IEnumerable<TeamViewModel>>> List(int id) {
    var team = await _context.Teams
        .Where(c => c.TeamId == id)
        .Select(c => new TeamViewModel { //<--
            TeamId = c.TeamId,
            TeamName = c.Team.Name
        })
        .ToListAsync();

    if (team.Count == 0) {
        return NotFound();
    }

    return team;
}

Note your query wont return null so no need to check for that.

If the list is empty then that should indicate that there are no records.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

ASP.NET MVC 5 Web API not accepting View Model on POST from MVC ASP.NET Core solution

分類Dev

ASP.NET Core MVC - Trouble returning model with view

分類Dev

Return "raw" json in ASP.NET Core 2.0 Web Api

分類Dev

Return string from Web API .NET Core get operation

分類Dev

Regarding return view with model usage

分類Dev

Return API result to view

分類Dev

Return API result to view

分類Dev

Update model with TryUpdateModel in ASP.NET MVC Core Web API and Entity Framework Core doesn't work

分類Dev

pass a symbol or special characters from model to view in asp.net core

分類Dev

View(model:MyModel);を返します。ASP.Net Core RazorPagesで同等

分類Dev

.NET Core API LoggerFactory

分類Dev

Return FirstName from model to a label in View

分類Dev

How to return a 304 result from ASP.NET Core web api?

分類Dev

How to return 404 on wrong API url? (ASP.NET Core + SPA)

分類Dev

Return complete XML response from ASP .NET Core Web API method

分類Dev

Return json to partial view in core 2

分類Dev

Asp.net Core MVC View @ model行がブラウザーに出力されます

分類Dev

ASP.NET MVC view model rendering

分類Dev

How to implement Custom Model Validator in .NET Core

分類Dev

TypeScript API in .NET Core version

分類Dev

.Net Core API method not calling

分類Dev

.net core 2.1 CORS api

分類Dev

InsertOne mongodb in .net core does not return result

分類Dev

Model binding not working in aspnet core web api

分類Dev

How to Pass View Data to Partial View in Asp.net core?

分類Dev

Strongly typed list return from View to fill Model?

分類Dev

.NET Core Web APIキー

分類Dev

ASP.Net Core API Accessing HttpRequestMessage

分類Dev

.Net Core API Returning StreamContent with request object

Related 関連記事

  1. 1

    ASP.NET MVC 5 Web API not accepting View Model on POST from MVC ASP.NET Core solution

  2. 2

    ASP.NET Core MVC - Trouble returning model with view

  3. 3

    Return "raw" json in ASP.NET Core 2.0 Web Api

  4. 4

    Return string from Web API .NET Core get operation

  5. 5

    Regarding return view with model usage

  6. 6

    Return API result to view

  7. 7

    Return API result to view

  8. 8

    Update model with TryUpdateModel in ASP.NET MVC Core Web API and Entity Framework Core doesn't work

  9. 9

    pass a symbol or special characters from model to view in asp.net core

  10. 10

    View(model:MyModel);を返します。ASP.Net Core RazorPagesで同等

  11. 11

    .NET Core API LoggerFactory

  12. 12

    Return FirstName from model to a label in View

  13. 13

    How to return a 304 result from ASP.NET Core web api?

  14. 14

    How to return 404 on wrong API url? (ASP.NET Core + SPA)

  15. 15

    Return complete XML response from ASP .NET Core Web API method

  16. 16

    Return json to partial view in core 2

  17. 17

    Asp.net Core MVC View @ model行がブラウザーに出力されます

  18. 18

    ASP.NET MVC view model rendering

  19. 19

    How to implement Custom Model Validator in .NET Core

  20. 20

    TypeScript API in .NET Core version

  21. 21

    .Net Core API method not calling

  22. 22

    .net core 2.1 CORS api

  23. 23

    InsertOne mongodb in .net core does not return result

  24. 24

    Model binding not working in aspnet core web api

  25. 25

    How to Pass View Data to Partial View in Asp.net core?

  26. 26

    Strongly typed list return from View to fill Model?

  27. 27

    .NET Core Web APIキー

  28. 28

    ASP.Net Core API Accessing HttpRequestMessage

  29. 29

    .Net Core API Returning StreamContent with request object

ホットタグ

アーカイブ