如何在MVC中完成搜索后从搜索文本框中清除文本

时间:2014-12-02 16:20:30

标签: asp.net-mvc

我有两个下拉列表和两个文本框

Search By: ByHtml.DropDownList("Search1", "Please Select...")
        Html.TextBox("searchString1")    

Search By: Html.DropDownList("Search2", "Please Select...")
        @Html.TextBox("searchString2")

        <input type="submit" value="Filter" />

当我从任何DDL进行选择并在文本框中键入文本并点击过滤器时我的搜索返回,但是在搜索后文本仍保留在文本框中,是否有一种方法可以在搜索后清除它,以便文本框是又空了?我试过了

ModelState.Remove("");

但它不起作用。

来自我的控制器代码的示例是

public class MainController : Controller
{
    private DBEntities db = new DBEntities();

    // GET: /Main/
    public ActionResult Index(string searchString1, string searchString2, string Search1, string Search2)
    {
       //Create a Dropdown list
        var SearchOptionList = new List<string>();
        SearchOptionList.Add("LandLord");
        SearchOptionList.Add("Postcode");
        SearchOptionList.Add("Street Address");
        ViewBag.Search1 = new SelectList(SearchOptionList);
        ViewBag.Search2 = new SelectList(SearchOptionList);

var mylist = from m in "mydatabase" select m;

//This statement runs if the user selects a parameter from Search2 and leaves Search1 empty
        if (String.IsNullOrEmpty(Search1) && !String.IsNullOrEmpty(Search2))
        {
            if (Search2 == "Postcode")
            {
                mylist = mylist.Where(s => s.Postcode.Contains(searchString2));
            }
            if (Search2 == "LandLord")
            {
                mylist = mylist.Where(s => s.Name.Contains(searchString2));
            }
            if (Search2 == "Street Address")
            {
                mylist = mylist.Where(s => s.StreetAddress.Contains(searchString2));
            }
        }

  return View(mylist.ToList());

}

3 个答案:

答案 0 :(得分:0)

您的视图模型应包含属性searchString1searchString2以及选择列表

public class SearchVM
{
  public string searchString1 { get; set; }
  public string searchString2 { get; set; }
  public SelectList SearchList1 { get; set; }
  public SelectList SearchList2 { get; set; }
}

控制器

public ActionResult Search()
{
  SearchVM model = new SearchVM();
  model.SearchList1 = new SelctList(...);
  model.SearchList2 = new SelctList(...);
  return View(model);
}

查看

@model SearchVM
@using(Html.BeginForm())
{
  ....
  @Html.DropDownListFor(m => m.searchString1, Model.SearchList1, "--Please select--")
  @Html.DropDownListFor(m => m.searchString2, Model.SearchList2, "--Please select--")
  ....
}

发布

[HttpPost]
public ActionResult Search(SearchVM model)
{
  // to clear all modelstate and reset values
  ModelState.Clear();
  model.searchString1 = null;
  model.searchString2 = null;
  // or to clear just one property and reset it
  ModelState.Remove("searchString1");
  model.searchString1 = null;
  // repopulate select lists if your returning the view
  return View(model);
}

答案 1 :(得分:0)

public ActionResult Index方法结束时但在return View()之前,我放置了以下完美无缺的代码

 ModelState.Remove("searchString1");
        ModelState.Remove("searchString2");
        ModelState.Remove("Search1");
        ModelState.Remove("Search2");

答案 2 :(得分:0)

我知道这是一个老问题,但是我遇到了同样的问题。所以我提出了解决方案。

查看:

@Html.TextBox("Search", null, new { @autofocus = "autofocus" })

控制器:

ViewBag.Search= null;
ModelState.Remove("Search");
return View(list.ToList());

希望能帮助别人