回帖后保留html输入值?

时间:2013-03-17 04:50:38

标签: asp.net asp.net-mvc asp.net-mvc-4

我的Asp.Net MVC 4在索引视图中包含以下代码。

<form action="/Deal" method="GET">
    <p>
        @Html.ActionLink("Create New", "Create")
        <input type="text" name="search"/>
        <input type="submit" value="Search" />
    </p>
</form>

以及DealController中的以下代码。但是,回发后html搜索输入是emtpy。在输入框中显示键入的搜索字符串的最佳方法是什么,以便用户可以继续修改它?

public ActionResult Index(string search)
{
    var deals = Deals.Include(d => d.Address);
    if (search != null)
        deals =
            deals.Where(d => d.Address.ApartmentNum.Contains(search) 
                             || d.Address.StreetAddress.Contains(search)
                             || d.Address.City.Contains(search) 
                             || d.Address.State == search 
                             || d.Address.Zip == search);
    return View(deals.ToList());
}

2 个答案:

答案 0 :(得分:1)

也许不是最好的,但更简单的方法之一。

 <form action="/Deal" method="GET">
     <p>
         @Html.ActionLink("Create New", "Create")
         <input type="text" name="search" value= "@((string)ViewBag.Search)" />
         <input type="submit" value="Search" />
     </p> 
</form>

public ActionResult Index(string search)
{
    var deals = Deals.Include(d => d.Address);
    if (search != null)
        deals =
            deals.Where(d => d.Address.ApartmentNum.Contains(search) 
                             || d.Address.StreetAddress.Contains(search)
                             || d.Address.City.Contains(search) 
                             || d.Address.State == search 
                             || d.Address.Zip == search);
    ViewBag.Search = search;
    return View(deals.ToList());
}

答案 1 :(得分:0)

发现只需要使用旧方式

<input type="text" name="search" value="@Request.QueryString["search"]"/>