2

i am working on a project in which i have to display some webgrids. Those webgrids have the same datasource but they should be divided based on some condition. Here is my code for controller:

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

And this is my view :

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

As you can see from the controller code, i have a parameter called AdType there are only 2 values possible for that : 1 and 2. Now i want to display two webgrids, one which has AdType value 1 and the other 2.Both the webgrids have the same data source. Thank you

1 Answer 1

2

You can filter your model with a linq query:

@{
     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"
                                          );  
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.