我有一个问题,我必须从数据库中检索数据并以网格格式显示它 cshtml....我有数据在视图中,但它没有显示我收到错误这是我的代码的样子
BugTracker Model
namespace Gridview_BugTracker.Models
{
public class BugTracker_DataHelper
{
public static List<BugTracker_DataHelper> GetList{get;set;}
public string projectName { get; set; }
public string Description { get; set; }
public string status { get; set; }
}
------------------------------
BugTracker Controller
public ActionResult Index()
{
Gridview_BugTracker.Models.BugTracker_DataHelper model = new BugTracker_DataHelper();
SqlConnection conn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BugTracker;Data Source=SSDEV6\SQLEXPRESS");
conn.Open();
SqlCommand dCmd = new SqlCommand("Select * from Projects", conn);
SqlDataAdapter da = new SqlDataAdapter(dCmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
model.projectName = ds.Tables[0].Rows[i]["projectName"].ToString();
model.Description = ds.Tables[0].Rows[i]["Description"].ToString();
model.status = ds.Tables[0].Rows[i]["Status"].ToString();
}
return View(model);
}
Index.Cshtml
@model IEnumerable<Gridview_BugTracker.Models.BugTracker_DataHelper>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
ProjectName
</th>
<th>
Description
</th>
<th>
Status
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.projectName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.status)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.projectName }) |
@Html.ActionLink("Details", "Details", new { id = item.Description }) |
@Html.ActionLink("Delete", "Delete", new { id = item.status })
</td>
</tr>
}
执行程序时出现此错误
error
----------------
The model item passed into the dictionary is of type 'Gridview_BugTracker.Models.BugTracker_DataHelper',
but this dictionary requires a model item of type
'System.Collections.Generic.IEnumerable`1[Gridview_BugTracker.Models.BugTracker_DataHelper]'.
所以任何人都可以帮助我在哪里做错了,或者我还需要添加什么......