Mvc3: Html.beginform Search Returning Empty Querystring
In an MVC3 app, i have the following View: @using (Html.BeginForm('Index', 'Search', new {query = @Request.QueryString['query']}, FormMethod.Post)) {
Solution 1:
The Problem is that you are look in the Request.QueryString
collection. But you are doing a POST
so the query
value is in the Request.Form
Collection. But i think you want your TextBox filled with the data so can do it like in my sample.
Sample
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<input type="search" name="query" id="query" value="@Request.Form["query"]" />
}
But this is not the real MVC approach. You should create a ViewModel for that.
Model
namespace MyNameSpace.Models
{
public class SearchViewModel
{
public string Query { get; set; }
}
}
View
@model MyNameSpace.Models.SearchViewModel
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
@Html.TextBoxFor(x => x.Query)
<input type="submit" />
}
Controller
public ActionResult Index()
{
return View(new SearchViewModel());
}
[HttpPost]
public ActionResult Index(SearchViewModel model)
{
// do your search
return View(model);
}
Post a Comment for "Mvc3: Html.beginform Search Returning Empty Querystring"