我的 Razor 页面遇到问题,数据表未显示任何数据。有人可以帮我排除故障并解决这个问题吗?
带有标题的表格会显示,但实际数据不会输出。
尝试过: 命名空间 LibraryApp.Pages { 公共类管理:PageModel {
public List<Signup_data> listUsers = new List<Signup_data>();
public void OnGet()
{
try
{
string connectionString = "Data Source=.\\sqlexpress;Initial Catalog=Library;Integrated Security=True;Trust Server Certificate=True";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string sql = "SELECT * FROM login";
using (SqlCommand command = new SqlCommand(sql, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Signup_data data = new Signup_data();
data.id = reader.GetInt32(0);
data.firstname = reader.GetString(1);
data.lastname = reader.GetString(2);
data.email = reader.GetString(3);
data.password = reader.GetString(4);
listUsers.Add(data);
}
}
}
}
}
catch (Exception ex)
{
}
}
}
public class Signup_data
{
public int id;
public string firstname;
public string lastname;
public string email;
public string password;
}
}
<!-- DATABASE connection -->
<h2>List of Users</h2>
<a class='btn btn-primary btn-sm' href='/Data/Create'>New user</a>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Password</th>
</tr>
</thead>
<tbody>
@foreach (var data in Model.listUsers)
{
<tr>
<td>@data.id</td>
<td>@data.firstname</td>
<td>@data.lastname</td>
<td>@data.email</td>
<td>@data.password</td>
<!-- Buttons-->
<td>
<a class="btn btn-primary btn-sm" href="/Data/[email protected]">Edit</a>
<a class="btn btn-danger btn-sm" href="/Data/[email protected]">Delete</a>
</td>
</tr>
}
</tbody>
</table>
删除
Trust Server Certificate=True
中的connectionString
:
string connectionString = "Data Source=.\\sqlexpress;Initial Catalog=Library;Integrated Security=True;";