if else条件在mvc中显示webgrid

时间:2013-03-07 09:15:58

标签: asp.net-mvc razor webgrid

我正在开展一个项目,我必须在其中展示一些webgrids。这些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.现在我想显示两个webgrids,一个有AdType值1和其他2. 两个webgrids都具有相同的数据源。谢谢

1 个答案:

答案 0 :(得分: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"
                                          );  
}