2012-07-12 5 views
0

나는 문제가있다. 데이터베이스에서 데이터를 가져 와서 표 형식으로 cshtml을 보여줘야한다. .... 데이터를 볼 수는 있지만 코드가 보이지 않는다. 내가 프로그램을 실행할 때MVC3에서 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]'. 
,

그래서 내가 잘못하고 오전 또는 내가 더 어떤 일을 추가해야 할 하나 개의 도움이 .....

당신이 Gridview_BugTracker.Models.BugTracker_DataHelper와보기를 보낼 수 있기 때문이다

답변

1

과 같아야합니다 aHelper 객체. 따라서 객체 목록을 View에 반환하십시오. 지금

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

을 필요로 할 때 여러 위치에서 호출 액션이 메소드를 호출 할 수 있도록 제가 데이터베이스를 이동할 것

는 seprate 방법에 코드를 액세서

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

몇 가지 더 제안 또한

1) 코드에 적절한 예외 처리 추가

2)Select *Select specific column name 형식으로 변경하십시오.

3) DataRow 셀 값에서 함수 (이 예제에서는 ToString())에 액세스하여 호출하기 전에 null을 확인하십시오. 당신은 단순히 항목의 목록을 읽는 경우

4)사용 DataReader를 대신 데이터 집합

+0

그것은 많은 ........... – SoftwareNerd

+0

@anilkumar를 감사했다 : 당신은 환영. 기꺼이 도와 드릴 수 있습니다. :) – Shyju

0

IEnumerable<Gridview_BugTracker.Models.BugTracker_DataHelper>

기대할 수 있습니다 보기는 BugTracker_Dat의 목록을 기대 어디 이 BugTracker_DataHelpe 객체의 하나 개의 인스턴스를 반환하기 때문에 컨트롤러의 코드는

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); 
    } 
관련 문제