2013-03-07 51 views
2

我正在一個項目中,我必須顯示一些webgrids。那些webgrid具有相同的數據源,但應根據某些條件進行劃分。這裏是我的控制器代碼:如果其他條件在視圖中顯示webvrid mvc

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. 兩個webgrid都有相同的數據源。謝謝

回答

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" 
             ); 
} 
+0

感謝您的代碼工作完美 – nitinvertigo 2013-03-07 09:26:57

相關問題