简体   繁体   English

在类型“System.Collections.Generic.List”中发现多个构造函数错误

[英]Multiple Constructors Error found in type 'System.Collections.Generic.List'

Error错误

InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'System.Collections.Generic.List`1[hdsportal.Pages.GestaoAlertas]'. InvalidOperationException:在类型“System.Collections.Generic.List`1[hdsportal.Pages.GestaoAlertas]”中发现了接受所有给定参数类型的多个构造函数。 There should only be one applicable constructor.应该只有一个适用的构造函数。

View of the page:页面视图:

    @page
    @model List<GestaoAlertas>
    
    @{
        ViewData["Title"] = "Gestao_Alertas";
        string[] TableHeaders = new string[] {"ID"
                ,"SYSTEM NAME"
                ,"SYSTEM STATUS"
                ,"SYSTEM SHORTMSG"};
    }
    
    <!-- Tabela onde vai estar a gestão dos alertas contido no INDEX -->
    
    <div class="bg-light text-center bg-light rounded border border-dark m-4">
        <div class="col-md-12">
            <h1 class="display-4 text-center p-4">Gestão de Alertas</h1>
            <table class="table table-bordered table-hover text-center p-4 border border-dark">
                <thead>
                    <!--Parte de cima da tabela -->
    
                    <tr class="table-success disabled">
                        @{
                            foreach (var head in TableHeaders)
    
                            {
                                <th style="width: 5%" scope="col">
                                    @head
                                </th>
    
                            }
                        }
    
                    </tr>
                </thead>
                <tbody>
                    <!-- Conteudo da tabela -->
                    @{
                        if (Model != null)
                        {
    
                            foreach (var Data in Model)
                            {
                                <tr>
                                    <td>@Data.ID</td>
                                    <td>@Data.SYSTEM_NAME</td>
                                    <td>@Data.SYSTEM_STATUS</td>
                                    <td>@Data.SYSTEM_SHORTMSG</td>
                                </tr>
                            }
                        }
                    }
                </tbody>
            </table>
        </div>
    </div> 

在此处输入图像描述

Controller used: Controller 使用过:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Data.SqlClient;

namespace hdsportal.Pages
{
    public class HomeController : Controller
    {
        SqlCommand com = new SqlCommand();
        SqlDataReader dr;
        SqlConnection con = new SqlConnection();
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
            con.ConnectionString = "secret";
        }

        public IActionResult Gestao_Alertas()
        {
            var addresses = FetchData();
            return View(addresses);
        }

        private List<GestaoAlertas> FetchData()
        {
            List<GestaoAlertas> addresses = new List<GestaoAlertas>();

            if (addresses.Count > 0)
            {
                addresses.Clear();
            }
            try
            {
                con.Open();
                com.Connection = con;
                com.CommandText = "SELECT [ID], [SYSTEM_NAME], [SYSTEM_STATUS], [SYSTEM_SHORTMSG] FROM [CORE_SYS_STATUS]";
                dr = com.ExecuteReader();
                while (dr.Read())
                {
                    addresses.Add(new GestaoAlertas()
                    {
                        ID = dr["ID"].ToString(),
                        SYSTEM_NAME = dr["SYSTEM_NAME"].ToString(),
                        SYSTEM_STATUS = dr["SYSTEM_STATUS"].ToString(),
                        SYSTEM_SHORTMSG = dr["SYSTEM_SHORTMSG"].ToString()
                    });

                }
                con.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return addresses;
        }
    }
}

Model Used: Model 使用:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Diagnostics;
                
namespace hdsportal.Pages
{           
    public class GestaoAlertas : PageModel
    {
        public string ID { get; set; }
        public string SYSTEM_NAME { get; set; }
        public string SYSTEM_STATUS { get; set; }
        public string SYSTEM_SHORTMSG { get; set; }               
    }
}    

I think I'm using razor pages MVC instead of MVC and that might be the source of the problem, but I'm not interily sure, anyone have any solutions?我想我正在使用 razor 页 MVC 而不是 MVC,这可能是问题的根源,但我不确定,有人有任何解决方案吗?

Looks like you cannot use "ambiguous" types as model for the razor pages.看起来您不能将“模糊”类型用作 model 页面的 razor。 Check out this thread in as.netcore repoas.netcore 存储库中查看此线程

You can try to put List<GestaoAlertas> into Pages/Gestao_Alertas.cshtml.cs page model.您可以尝试将List<GestaoAlertas>放入 Pages/Gestao_Alertas.cshtml.cs 页面 model。

Model: Model:

public class GestaoAlertas 
    {
        public string ID { get; set; }
        public string SYSTEM_NAME { get; set; }
        public string SYSTEM_STATUS { get; set; }
        public string SYSTEM_SHORTMSG { get; set; }               
    }

Pages/Gestao_Alertas.cshtml.cs page model Pages/Gestao_Alertas.cshtml.cs 页面 model

public class Gestao_AlertasModel : PageModel
    {
        [BindProperty]
        public List<GestaoAlertas> list { get; set; }=new List<GestaoAlertas>();
        public void OnGet()
        {
            //set List<GestaoAlertas> here
        }
    }

Pages/Gestao_Alertas.cshtml:页面/Gestao_Alertas.cshtml:

@page
@model {YourProjectName}.Pages.Gestao_AlertasModel
@{
    ViewData["Title"] = "Gestao_Alertas";
    string[] TableHeaders = new string[] {"ID"
                ,"SYSTEM NAME"
                ,"SYSTEM STATUS"
                ,"SYSTEM SHORTMSG"};
}

<!-- Tabela onde vai estar a gestão dos alertas contido no INDEX -->

<div class="bg-light text-center bg-light rounded border border-dark m-4">
    <div class="col-md-12">
        <h1 class="display-4 text-center p-4">Gestão de Alertas</h1>
        <table class="table table-bordered table-hover text-center p-4 border border-dark">
            <thead>
                <!--Parte de cima da tabela -->

                <tr class="table-success disabled">
                    @{
                        foreach (var head in TableHeaders)

                        {
                            <th style="width: 5%" scope="col">
                                @head
                            </th>

                        }
                    }

                </tr>
            </thead>
            <tbody>
                <!-- Conteudo da tabela -->
                @{
                    if (Model != null)
                    {

                        foreach (var Data in Model.list)
                        {
                            <tr>
                                <td>@Data.ID</td>
                                <td>@Data.SYSTEM_NAME</td>
                                <td>@Data.SYSTEM_STATUS</td>
                                <td>@Data.SYSTEM_SHORTMSG</td>
                            </tr>
                        }
                    }
                }
            </tbody>
        </table>
    </div>
</div>

暂无
暂无

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

相关问题 无法隐式转换类型&#39;System.Collections.Generic.List错误 - Cannot implicitly convert type 'System.Collections.Generic.List error C# SelectMany 错误“无法隐式转换类型‘System.Collections.Generic.List<char> ' 到 'System.Collections.Generic.List<list> ’”</list></char> - C# SelectMany error " Cannot Implicitly convert type 'System.Collections.Generic.List<char>' to 'System.Collections.Generic.List<List>' " 无法隐式转换类型&#39;System.Collections.Generic.list <fooClass> &#39;到&#39;System.Collections.Generic.List <T> - Cannot Implicitly convert type 'System.Collections.Generic.list<fooClass>' to 'System.Collections.Generic.List<T> 无法隐式转换类型&#39;System.Collections.Generic.List <AnonymousType#1> &#39;到&#39;System.Collections.Generic.List - Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List 键入“对象{System.Collections.Generic.List <T> }“ VS” System.Collections.Generic.List <T> ” - Type “object {System.Collections.Generic.List<T>}” VS “System.Collections.Generic.List<T>” 无法隐式转换类型&#39;System.Collections.Generic.List <MyProduct> &#39;到&#39;System.Collections.Generic.List <T> - Cannot implicitly convert type 'System.Collections.Generic.List<MyProduct>' to 'System.Collections.Generic.List<T> 如何修复“无法将类型System.Collections.Generic.List &lt;&gt;隐式转换为System.Collections.Generic.List &lt;&gt;” - How to fix 'Cannot implicitly convert type System.Collections.Generic.List<> to System.Collections.Generic.List<>' 无法隐式转换类型'System.Collections.Generic.List<x> ' 到 'System.Collections.Generic.List<y> '</y></x> - Cannot implicitly convert type 'System.Collections.Generic.List<x>' to 'System.Collections.Generic.List<y>' 该类型不能隐式转换 system.collections.generic.list 到 system.collections.generic.list - The type cannot be converted implicitly system.collections.generic.list to system.collections.generic.list 错误使用泛型类型&#39;System.Collections.Generic.List&#39;需要&#39;1&#39;类型参数 - Error Using the generic type 'System.Collections.Generic.List' requires '1' type arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM