0

我有一个问题,我必须从数据库中检索数据并以网格格式显示它 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]'. 

所以任何人都可以帮助我在哪里做错了,或者我还需要添加什么......

4

2 回答 2

1

因为您只返回一个 BugTracker_DataHelpe 对象实例,而视图需要一个BugTracker_DataHelper 对象列表。所以返回一个对象列表给视图。

我会将数据库访问代码移动到一个单独的方法中,以便我可以在需要时从多个地方调用它

public List<BugTracker_DataHelper> GetAllBugs()
{
    var modelList = new List<BugTracker_DataHelper>();
    using(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);         
      for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
      {
        var model = new BugTracker_DataHelper();
        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();
        modelList.Add(model);
     }
    }
    return modelList;
}

现在在您的操作中调用此方法

public ActionResult Index()
{
   var bugList=GetAllBugs();
   return View(bugList);    
}

还有一些建议

1)在代码中添加适当的异常处理

2)更改Select *Select specific column name格式。

3)ToString()在访问和调用DataRow 单元格值上的函数(在本例中)之前检查 null 。

4)如果您只是阅读项目列表,请使用 DataReader 而不是 DataSet

于 2012-07-12T14:26:45.237 回答
0

那是因为您发送 aGridview_BugTracker.Models.BugTracker_DataHelper并且视图期望 aIEnumerable<Gridview_BugTracker.Models.BugTracker_DataHelper>

您在控制器中的代码应如下所示

public ActionResult Index()
    {
        List<Gridview_BugTracker.Models.BugTracker_DataHelper> model = new List<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++)
        {
            Gridview_BugTracker.Models.BugTracker_DataHelper item = new BugTracker_DataHelper()
            item.projectName = ds.Tables[0].Rows[i]["projectName"].ToString();
            item.Description = ds.Tables[0].Rows[i]["Description"].ToString();
            item.status = ds.Tables[0].Rows[i]["Status"].ToString();

            model.add(item);
        }


        return View(model);
    }
于 2012-07-12T14:26:32.037 回答