SoftwareNerd
SoftwareNerd

Reputation: 1895

How to show the data in a grid in cshtml in MVC3?

I have a problem,i have to retrieve the data from database and show it cshtml in a grid format....i have the data in view,but its not showing the i am geting an error this is how my code looks

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

I am getting this error when i execute the program

            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]'. 

so can any one help where am i doing wrong or do i have to add any thing more.....

Upvotes: 0

Views: 6239

Answers (2)

Shyju
Shyju

Reputation: 218732

Because you are returning only one instance of BugTracker_DataHelpe object where the View expects a List of BugTracker_DataHelper objects. So return a list of objects to the View.

I would move the database acces code to a seprate method so that i can call it from multiple places when needed

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

Now call this method in your Action

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

Few more suggestions also

1) Add proper exception handling to the code

2) Change the Select * to Select specific column name format.

3) Check for null before accessing and calling functions(ToString() in this example) on the DataRow cell values.

4) If you are simply reading the list of items, Use DataReader instead of DataSet

Upvotes: 1

Zach dev
Zach dev

Reputation: 1620

that's because you send a Gridview_BugTracker.Models.BugTracker_DataHelper and the view expect a IEnumerable<Gridview_BugTracker.Models.BugTracker_DataHelper>

your code in the controller should look like

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

Upvotes: 0

Related Questions