0

错误

InvalidOperationException:在类型“System.Collections.Generic.List`1[hdsportal.Pages.GestaoAlertas]”中找到了接受所有给定参数类型的多个构造函数。应该只有一个适用的构造函数。

页面视图:

    @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> 

在此处输入图像描述

使用的控制器:

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;
        }
    }
}

使用型号:

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; }               
    }
}    

我想我使用的是剃须刀页面 MVC 而不是 MVC,这可能是问题的根源,但我不确定,有人有任何解决方案吗?

4

2 回答 2

0

您可以尝试放入List<GestaoAlertas>Pages/Gestao_Alertas.cshtml.cs 页面模型。

模型:

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 页面模型

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

页面/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>
于 2021-10-07T06:18:18.217 回答
0

看起来您不能使用“模糊”类型作为剃刀页面的模型。在aspnetcore 存储库中查看此线程

于 2021-10-06T15:05:29.850 回答