Reputation: 1074
I am trying to create a search box to look for invoices in database. The code for search box is as below:
@using (Ajax.BeginForm("Search", "Invoice", new AjaxOptions() { HttpMethod = "POST" }))
{
<% input id="search-field" name="search" type="text" value="" %/>
<% input id="search-submit" name="search-submit" type="submit" value="" %/>
}
public ActionResult Search(FormCollection collection)
{
if (collection["search-field"] == null)
return RedirectToAction("Index");
else
{
string id = collection["search-field"].ToString();
return RedirectToAction("Details", "Invoice", id.Trim());
}
}
Now the problem is that I receive only null values the controller search action.
I am using MVC3 and .NET framework 4.0
I still can not receive the string value while catching the string in next action:
public ActionResult Details(string id) {
if(string.IsNullOrEmpty(id))
return RedirectToAction("Index"); ==============> Here
ObjectParameter[] parameters = new ObjectParameter[3];
parameters[0]= new ObjectParameter("CUSTNMBR", id);
parameters[1] = new ObjectParameter("StartDate", System.DateTime.Now.Date.AddDays(-90));
parameters[2] = new ObjectParameter("EndDate", System.DateTime.Now.Date);
return View(_db.ExecuteFunction<Models.Invoices>("uspGetCustomerInvoices", parameters).ToList<Models.Invoices>());
}
Upvotes: 0
Views: 256
Reputation: 5800
The main issue is that you are searching the FormCollection
based on the id
of the input
elements instead of their name
attribute. Try writing your code like this:
View:
@using (Ajax.BeginForm("Search", "Invoice", new AjaxOptions() { HttpMethod = "POST" }))
{
<input id="search-field" name="search" type="text" value="" />
<input id="search-submit" name="search-submit" type="submit" />
}
Action:
public ActionResult Search(string search)
{
if (string.IsNullOrEmpty(search))
return RedirectToAction("Index");
return RedirectToAction("Details", "Invoice", search.Trim());
}
I modified your action so you no longer need to query the FormCollection
Upvotes: 1