1

我的 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 搜索输入是空的。在输入框中显示键入的搜索字符串以便用户继续修改它的最佳方式是什么?

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());
}
4

2 回答 2

2

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

 <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());
}
于 2013-03-17T04:58:18.603 回答
1

发现只需要用老办法

<input type="text" name="search" value="@Request.QueryString["search"]"/>
于 2013-03-17T06:17:14.070 回答