Reputation: 29159
My Asp.Net MVC 4 has the following code in the Index View.
<form action="/Deal" method="GET">
<p>
@Html.ActionLink("Create New", "Create")
<input type="text" name="search"/>
<input type="submit" value="Search" />
</p>
</form>
And the following code in the DealController. However, the html search input is emtpy after post back. What's the best way to display the typed search string in the input box so users can continue modify it?
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());
}
Upvotes: 1
Views: 7209
Reputation: 29159
Found just need to use the old way
<input type="text" name="search" value="@Request.QueryString["search"]"/>
Upvotes: 1
Reputation: 3659
Perhaps not the best, but one of the easier ways.
<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());
}
Upvotes: 2