简体   繁体   English

在 ASP.NET Core MVC Web 应用程序中出现构造函数错误?

[英]Getting constructor error in ASP.NET Core MVC web app?

I keep getting the error below when trying to pass my IEnumerable to the view.尝试将IEnumerable传递给视图时,我不断收到以下错误。

I'm really puzzled in what is it, the return type of GetCandidates is IEnumerable<Candidate> .我真的很困惑它是什么, GetCandidates的返回类型是IEnumerable<Candidate>

InvalidOperationException: A suitable constructor for type 'System.Collections.Generic.ICollection`1[Models.Candidate]' could not be located. InvalidOperationException:找不到适合类型“System.Collections.Generic.ICollection`1[Models.Candidate]”的构造函数。 Ensure the type is concrete and all parameters of a public constructor are either registered as services or passed as arguments.确保类型是具体的,并且公共构造函数的所有参数要么注册为服务,要么作为参数传递。 Also ensure no extraneous arguments are provided.还要确保没有提供无关的参数。

My Candidate model class:我的Candidate模型类:

public class Candidate
{
    public Guid candidateId { get; set; }
    public string fullName { get; set; }
    public bool seen { get;set; }
    public bool status { get; set; }

    public IEnumerable<Experience> experience { get; set; }
}

My registered services:我注册的服务:

builder.Services.AddHttpClient();
builder.Services.AddTransient<IApiService, ApiService>();
// other code //

My Index.cshtml view:我的Index.cshtml视图:

@page
@using Models

@model ICollection<Candidate>

@{
    ViewData["Title"] = "Home page";
}

//other code//

You need firstly learn how to get started in Razor Pages:您首先需要了解如何开始使用 Razor Pages:

https://docs.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-6.0&tabs=visual-studio#razor-pages https://docs.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-6.0&tabs=visual-studio#razor-pages

Razor Pages do not use @model ModelName ,it uses @model <PageName>Model ,which is different from MVC. Razor Pages 不使用@model ModelName ,它使用@model <PageName>Model ,这与 MVC 不同。

Change your Index.cshtml like below:更改您的Index.cshtml如下所示:

@page

@model IndexModel

@{
    ViewData["Title"] = "Home page";
}
@foreach(var item in Model.Candidate)
{
    @item.fullName
}

The Index.cshtml.cs like below: Index.cshtml.cs如下所示:

public class IndexModel : PageModel
{

    public List<Candidate> Candidate { get; set; }

    public void OnGet()
    {
        //for easy testing, I just hard-coded...
        Candidate = new List<Candidate>()
        {
            new Candidate(){fullName="aa"},
            new Candidate(){fullName="aa1"},
            new Candidate(){fullName="aa2"}
        };
        
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 ASP.NET Core 3 MVC Web 应用程序中设置路由 - Setting up routes in ASP.NET Core 3 MVC web app ASP.NET Core MVC web 应用程序在更新数据库时抛出错误 - ASP.NET Core MVC web app throws error while updating the database 将带有类库的ASP.Net Core Web App发布到Azure时在主页上出现500错误 - Getting 500 Error on Home page when publishing ASP.Net Core Web App with class library to Azure 我在 ASP.NET Core Web API 应用程序上配置 CORS 时出现 CORS 错误 - I'm getting CORS error with CORS configured on ASP.NET Core Web API app IIS .NET Core 5.0 MVC API - 获取 HTTP 错误 500.30 - ASP.NET Core 应用程序无法启动 - IIS .NET Core 5.0 MVC API - Getting HTTP Error 500.30 - ASP.NET Core app failed to start 每次启动ASP.NET Core MVC应用程序时出现IIS错误 - IIS error with every launch of an ASP.NET Core MVC app 将ASP.NET Core部署到Azure Web App服务错误 - Deploy ASP.NET Core to Azure Web App Service Error 从 Azure 中托管的 Asp.net Core MVC Web App 获取“响应状态代码并不表示成功:502(坏网关)” - Getting "Response status code does not indicate success: 502 (Bad Gateway)" from Asp.net Core MVC Web App hosted in Azure Azure中的ASP.NET MVC Core应用 - ASP.NET MVC Core app in Azure 具有ASP.NET MVC的数据库Web应用 - Database web app with ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM