【问题标题】:How to bind data to Grid in MVC3?如何在 MVC3 中将数据绑定到 Grid?
【发布时间】:2012-07-25 20:06:15
【问题描述】:

过去几天我一直在使用 MVC...

我必须在网格中显示一堆数据...这些天我设法在表格中显示它,但我的要求是将它绑定到 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>
   <%} %>

如果我可以在表格中进行分页,我该如何做到这一点,或者我应该如何在网格中显示他的数据,谁能帮助我......谁能解释一下如何做到这一点

【问题讨论】:

    标签: asp.net-mvc-3 pagination html-table webgrid


    【解决方案1】:

    您可以为此使用 Webgrid。

    这里有几个链接可以帮助您了解 webgrid 的工作原理

    webgrid 简介

    http://www.mikesdotnetting.com/Article/154/Looking-At-The-WebMatrix-WebGrid http://msdn.microsoft.com/en-us/magazine/hh288075.aspx

    webgrid 中的分页

    http://yassershaikh.com/webgrid-paging-with-pager-method-in-razor-mvc/

    Webgrid 中的高级(高效)分页

    http://www.dotnetcurry.com/ShowArticle.aspx?ID=615

    希望这会有所帮助..!

    【讨论】:

      【解决方案2】:

      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")
      ))
      

      【讨论】:

      • @anilkumar 将 "@{" 替换为
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-21
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      相关资源
      最近更新 更多