如何在MVC3中的cshtml中显示网格中的数据?

时间:2012-07-12 14:20:10

标签: asp.net-mvc-3 c#-4.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]'. 

所以任何人都可以帮助我在哪里做错了或者我必须添加更多的东西.....

2 个答案:

答案 0 :(得分:1)

因为你只返回一个BugTracker_DataHelpe对象的实例,其中View需要一个List 的BugTracker_DataHelper对象。因此,请将对象列表返回给View。

我会将数据库访问代码移动到seprate方法,以便我可以在需要时从多个位置调用它

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;
}

现在在Action

中调用此方法
public ActionResult Index()
{
   var bugList=GetAllBugs();
   return View(bugList);    
}

还有更多建议

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

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

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

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

答案 1 :(得分:0)

那是因为您发送了Gridview_BugTracker.Models.BugTracker_DataHelper,视图需要IEnumerable<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);
    }