MVC在页面之间导航时如何保持搜索值? [英] MVC How to keep search value when navigating between pages?

查看:66
本文介绍了MVC在页面之间导航时如何保持搜索值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个搜索框,它使用EMPLID进行搜索.搜索有效,但是如果我在搜索后转到任何其他页面(例如:如果我切换到详细信息页面),然后导航回到我进行搜索的页面,它将显示所有记录.如何保持搜索条件,以便当我在页面之间导航时显示该EMPLID的信息?

I have a search box that searches by using EMPLID. Search works but if I go to any other page after doing search (Ex: If I switch to details page) and then navigate back to the page I did search on, It displays all the records. How can I keep the search criteria so that when I navigate between pages it shows information of that EMPLID?

我的控制器:

public ActionResult Index(string SearchString)
{

    var emp = from e in db.EMPLOYMENTs
              select e;

    if (!String.IsNullOrEmpty(SearchString))
    {
        emp = emp.Where(s => s.EMPLID.Contains(SearchString));
    }

    return View(emp);
}

我的布局:

@using (Html.BeginForm())
{
    <form class="navbar-form navbar-left" role="search">
        <div class="form-group">
            <input class="form-control" placeholder="Search" type="text" name="SearchString">
        </div>
    </form>
}

推荐答案

您可以使用TempData保留搜索字符串.

You can use TempData for preserving the search string.

将您的SearchString添加到像这样的临时数据中

Add your SearchString in temp data like this-

TempData["SearchString"] = SearchString;

...并在需要时取回值-

...and get back the value when required-

string searchString = TempData["SearchString"] as string;

请参考 msdn 有关在ASP.NET MVC应用程序中传递数据的更多信息的文章

Please refer this msdn article for more information on passing Data in an ASP.NET MVC Application

这篇关于MVC在页面之间导航时如何保持搜索值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆