ASP.Net MVC显示/隐藏内容

时间:2014-11-05 01:35:39

标签: 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,直到有人按下提交按钮,然后它才能显示。

提前感谢您的帮助。

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

您可以尝试拥有一个标志(ViewBag.isShown = false;)
当页面刷新时,它将显示您的div。
代码应如下所示:

  

if(ViewBag.isShown == true)   {       ..你的每个街区..   }