错误
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,这可能是问题的根源,但我不确定,有人有任何解决方案吗?
