2

我正在做一个项目,我必须在其中显示一些 webgrids。这些网络网格具有相同的数据源,但应根据某些条件进行划分。这是我的控制器代码:

public ActionResult Index()
        {

            SqlCommand cmd = new SqlCommand("select * from Bspecial_Ad_management_tbl where ", con);
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            List<AddDetailModel> model = new List<AddDetailModel>();
            while (dr.Read())
            {
                model.Add(new AddDetailModel()
                {
                    AdName = dr["Ad_name"].ToString(),
                    AdType=dr["Ad_type_name"].ToString(),
                    PartnerName=dr["Partner Name"].ToString(),
                    hrefurl=dr["Ad_url"].ToString(),
                    startDate=dr["Start_date"].ToString(),
                    endDate = dr["End_date"].ToString(),
                    tagName = dr["Tag"].ToString(),
                    AdPath= dr["Ad_image_path"].ToString(),
                    Status = dr["Status"].ToString()
                });
            }
            dr.Close();
            return View(model);
        }

这是我的观点:

@model IEnumerable<EShopPartnerSetting.Models.AddDetailModel>
@{
   {
        Layout = "~/Views/Shared/AdminLayout.cshtml";
    }

}
@{
    var grid = new WebGrid(source: Model,
                                              defaultSort: "First Name",
                                              rowsPerPage: 5, fieldNamePrefix: "wg_",
                                              canPage: true, canSort: true,
                                              pageFieldName: "pg", sortFieldName: "srt"
                                              );  
}
<html>
<head>
   Some unrelated scripts here
</head>
<body>
    @using (Html.BeginForm())
    {
        @Html.ValidationSummary(true)
        <table width="960" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
                <td>
                    <div class="maindiv">
                        <div class="hd">
                            <h1>
                                Ad Management</h1>
                        </div>
                        <div class="bd">
                            <table align="center" cellpadding="0" cellspacing="0" width="100%">
                                <tr>
                                    <td>
                                        <input id="new" type="button" value="Create New Ad" style="color: #FFFFFF; background-color: #1688C6;" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        &nbsp;
                                    </td>
                                </tr>
                                <tr>
                                    <td align="center">
                                    <span>Bottom Banner</span>
                                        @grid.GetHtml(tableStyle: "listing-border", headerStyle: "gridhead", footerStyle: "paging", rowStyle: "td-dark", alternatingRowStyle: "td-light",
                            columns:
                                grid.Columns(
                                grid.Column("AdName", "Ad/Campaign", style: "colProductid"),
                                grid.Column("AdType", "Ad Type", style: "colProductRate"),
                                grid.Column(header: "Ad", format: @<text><img src="@item.AdPath"  id="adimg"  alt="YourText" title="Ad Image"  width:"50px" height="50px"></text>),
                                grid.Column("startDate", "Start Date", style: "colCategoryID"),
                                grid.Column("endDate", "End Date", style: "colCategoryID"),
                                grid.Column("tagName", "Tag", style: "colCategoryID"),
                                grid.Column("Status", "IsActive", style: "colCategoryID"),
                                grid.Column(header: "Edit", format: @<text><a id="@item.AdName" class="clk"><img
                                    src="../../Images/edit.png" class="asa" width="25px" height="25px" alt="" style="border: none;" /></a></text>, style: "colOperation"),
                                grid.Column(header: "Delete", format: @<text><a href="@Url.Action("Delete", "Ad", new { aname = item.AdName, apath = item.AdPath, status = item.Status })" onclick="javascript:return ConfirmDelete();"><img
                                    src="../../Images/delete.png" width="20px" height="20px" alt="" style="border: none;" /></a></text>, style: "colOperation"),
                                grid.Column(header: "Status", format: @<text>
                                        <input type="button" class="adbtn" name="one" style="color: #FFFFFF; background-color:#1688C6;" value="Change" onclick="window.location.href='@Url.Action("Add", "Ad", new { ids = item.AdType, path = item.AdPath, status = item.Status })';" /></text>, style: "colOperation")
                                ), mode: WebGridPagerModes.All)
                                    </td>
                                </tr>
                                    <tr>
                                    <td>
                                        &nbsp;
                                    </td>
                                </tr>
                                <tr>
                                    <td width="100%" align="center">
                                        @* <input id="Submit1" type="submit" value="submit" />*@
                                    </td>
                                </tr>
                            </table>
                        </div>
                        <script type="text/javascript">
                            function ConfirmDelete() {
                                return confirm("Are you sure you want to delete this?");
                            }
                        </script>
                    </div>
                    @* <a id="clk">click here</a>*@
                    <div id="dialog" title="Edit" style="overflow: hidden;">
                    </div>
                    <div id="newdialog" title="Create" style="overflow: hidden;">
                    </div>
                </td>
            </tr>
        </table>
    }
</body>
</html>

正如您从控制器代码中看到的那样,我有一个名为AdType的参数,只有 2 个可能的值:1 和 2。现在我想显示两个 webgrid,一个的 AdType 值为 1,另一个为 2。两个 webgrid具有相同的数据源。谢谢

4

1 回答 1

2

您可以使用 linq 查询过滤您的模型:

@{
     var grid1 = new WebGrid(source: Model.Where(m=>m.AdType=="1").ToArray(),
                                          defaultSort: "First Name",
                                          rowsPerPage: 5, fieldNamePrefix: "wg_",
                                          canPage: true, canSort: true,
                                          pageFieldName: "pg", sortFieldName: "srt"
                                          );  

     var grid2 = new WebGrid(source: Model.Where(m=>m.AdType=="2").ToArray(),
                                          defaultSort: "First Name",
                                          rowsPerPage: 5, fieldNamePrefix: "wg_",
                                          canPage: true, canSort: true,
                                          pageFieldName: "pg", sortFieldName: "srt"
                                          );  
}
于 2013-03-07T09:20:36.763 回答