2014-11-05 215 views
1

好吧,我有以下視圖ASP.Net MVC顯示/隱藏內容

@model IEnumerable<WebApplication3.Models.user> 

@{ 
    ViewBag.Title = "Password Management"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

@section title {<h1>@ViewBag.Title</h1>} 

<div id="page-block" class="page-block-three row"> 

    <div style="margin-top: 30px;" class="col-lg-offset-2 col-lg-8"> 
     @using (Html.BeginForm()) 
     { 
      <div class="input-group"> 
       @Html.TextBox("SearchString", null, new { @class = "form-control ccl-form", @style = "z-index: 10", @placeholder = "Enter Username"}) 
       <div class="input-group-btn"> 
        <button class="btn ccl-btn ccl-btn-red ccl-btn-search" type="submit"><i class="fa fa-search"></i></button> 
       </div> 
      </div> 
     } 
    </div> 

    <div class="col-lg-offset-3 col-lg-6"> 
     @foreach (var item in Model) 
     { 
      <div class="details-block"> 
       @Html.DisplayFor(modelItem => item.UserName) 
       <button type="button" class="btn ccl-btn ccl-btn-green ccl-btn-search pull-right">Select User</button> 
      </div> 

     } 
    </div> 

</div> 

我希望能夠做的是隱藏在下面的div

<div class="col-lg-offset-3 col-lg-6"> 
     @foreach (var item in Model) 
     { 
      <div class="details-block"> 
       @Html.DisplayFor(modelItem => item.UserName) 
       <button type="button" class="btn ccl-btn ccl-btn-green ccl-btn-search pull-right">Select User</button> 
      </div> 

     } 
    </div> 

那麼什麼表明div時點擊提交按鈕

我的控制器看起來像下面

public class PasswordController : Controller 
    { 
     private CCLPasswordManagementDBEntities db = new CCLPasswordManagementDBEntities(); 

     public ActionResult Search(string searchString) 
     { 
      var users = from x in db.users select x; 

      if (!String.IsNullOrEmpty(searchString)) 
      { 
       users = users.Where(x => x.UserName.ToUpper().Contains(searchString.ToUpper())); 
      } 

      return View(users); 
     } 

    } 

目前div不斷顯示並更新提交按鈕時按下,但我想隱藏該div,直到有人按下提交按鈕,然後它可以顯示。

在此先感謝您的幫助。

+0

所以,你希望它在表單提交和頁面刷新(從表單發佈)後顯示? – 2014-11-05 02:00:31

+1

當你提交你的渲染一個全新的視圖,所以你要做到這一點的唯一方法是使用ajax,或者通過向控制器傳遞額外的值來指示應顯示哪些項目並根據這些值呈現視圖( s) – 2014-11-05 02:01:32

+0

是的,我試圖在提交和刷新後顯示此內容 – 2014-11-05 02:39:23

回答

4

控制器更改您的密碼爲此:

public ActionResult Search(string searchString) 
    { 
     var users = from x in db.users select x; 
     ViewBag.ShowList = false; 
     if (!String.IsNullOrEmpty(searchString)) 
     { 
      ViewBag.ShowList = true; 
      users = users.Where(x => x.UserName.ToUpper().Contains(searchString.ToUpper())); 
     } 

     return View(users); 
    } 

,並更改您的視圖這樣的:

@model IEnumerable<WebApplication3.Models.user> 

@{ 
ViewBag.Title = "Password Management"; 
Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

@section title {<h1>@ViewBag.Title</h1>} 

<div id="page-block" class="page-block-three row"> 

<div style="margin-top: 30px;" class="col-lg-offset-2 col-lg-8"> 
    @using (Html.BeginForm()) 
    { 
     <div class="input-group"> 
      @Html.TextBox("SearchString", null, new { @class = "form-control ccl-form", @style = "z-index: 10", @placeholder = "Enter Username"}) 
      <div class="input-group-btn"> 
       <button class="btn ccl-btn ccl-btn-red ccl-btn-search" type="submit"><i class="fa fa-search"></i></button> 
      </div> 
     </div> 
    } 
</div> 
@if(ViewBag.ShowList){ 
<div class="col-lg-offset-3 col-lg-6"> 
    @foreach (var item in Model) 
    { 
     <div class="details-block"> 
      @Html.DisplayFor(modelItem => item.UserName) 
      <button type="button" class="btn ccl-btn ccl-btn-green ccl-btn-search pull-right">Select User</button> 
     </div> 

    } 
</div> 
} 
</div> 
0

你可以嘗試擁有一個標誌(ViewBag.isShown = false;)
當頁面刷新時,它會顯示你的div。
代碼應該是象下面這樣:

如果(ViewBag.isShown ==真){ ..your每個塊.. }