我想用我的数据库中的数据创建一个表(在我的视图中):
@model IEnumerable<project1.Models.Admin.LastLogins>
@{
ViewBag.Title = "Index";
Layout = "...";
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.time)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.time)
</td>
</tr>
</table>
我得到了错误(与foreach-loop一致):
System.NullReferenceException:“对象引用未设置为 一个对象实例。“
这是我的模特:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using MySql.Data.MySqlClient;
namespace Meilenstein3.Models.Admin
{
public class LastLogins
{
public string time { get; set; }
public static List<LastLogins> activities()
{
List<LastLogins> loginlist = new List<LastLogins>();
const string ConnStr= "...";
var query = "SELECT logintime FROM user JOIN;";
var connection = new MySqlConnection(ConnStr);
connection.Open();
var sqlcmdGast = new MySqlCommand(query, connection);
MySqlDataReader reader = sqlcmd.ExecuteReader();
while (reader.Read())
{
LastLogins a = new LastLogins();
a.time = reader["logintime"].ToString();
liste.Add(a);
}
reader.Close();
return liste;
任何人都能指出我为什么不工作? 感谢。