2012-07-27 69 views
3

過去幾天我一直在使用MVC ...如何在MVC3中將數據綁定到網格?

我必須在網格中顯示一堆數據......這些所有的日子我設法在一張表中顯示,但我的要求是綁定它的jQuery網格或的WebGrid ...

我堅持這一點,我不知道如何做到這一點期待的想法和建議....

控制器

 public ActionResult Index() 
    { 
     var bugList = GetList(); 
     return View(bugList); 
    } 
    public List<ProjectModel> GetList() 
    { 
     var modelList = new List<ProjectModel>(); 
     using (SqlConnection conn = new SqlConnection("Data Source=LMIT-0039;Initial Catalog=BugTracker;Integrated Security=True")) 
     { 
      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++) 
      { 
       var model = new ProjectModel(); 
       model.ID = Convert.ToInt16(ds.Tables[0].Rows[i]["ProjectID"]); 
       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; 
    } 

視圖(ASPX)

<table> 
<thead align="center"> 
<tr class="BoldCenterAlignHeaderStyle">  

    <th> 
     ProjectName 
    </th>  
    <th> 
     Status 
    </th> 
    <th align="center"> 
    Edit 
    </th>   
</tr> 
</thead> 
    <% foreach (var item in Model) { %> 
     <tr>   
     <td> 
     <%:Html.LabelForModel(item.projectName) %> 
     </td>  
     <td> 
     <%:Html.LabelForModel(item.status) %> 
     </td> 
     <td align="center"> 
     <a href="<%:Url.Action("Edit",new{id=item.ID}) %>" class="Edit"><img src="../../Content/edit.gif" height="8px"/></a> 
      <%--<%:Html.ActionLink("Edit", "Edit", new { id = item.ID })%> --%>   
     <%-- <a href="<%:Url.Action("Delete",new{id=item.ID}) %>" class="Delete"><img src="../../Content/delete.gif" height="8px" /></a>--%> 
     </td>   
     </tr> 
    <%} %> 

,如果我能在一個表中我如何能做到這一點,否則我應該如何顯示他在網格中的數據可以在任何一個可以幫助我,請做分頁... 。can any any explain me how to do this

+0

如何做到這一點的 – SoftwareNerd 2012-07-27 09:01:20

回答

1

舉例的WebGrid:

@model IEnumerable<ProjectModel> 
@{ 
    var grid = new WebGrid(
    source: Model, 
    rowsPerPage: 4); 
} 
@grid.GetHtml(
    tableStyle: "grid", 
    headerStyle: "header", 
    rowStyle: "row", 
    footerStyle: "footer", 
    alternatingRowStyle: "altRow", 
    columns: grid.Columns (
     grid.Column("projectName"), 
     grid.Column("ProjectID") 
)) 
+0

aspx頁面如何做到這一點的 – SoftwareNerd 2012-07-27 09:01:38

+0

aspx頁面@anilkumar取代 「@ {」 與<%和內<包裝你grind.GetHtml%。不要忘記用「<%@ Page」聲明替換「@model」。 – 2012-07-27 13:09:57