Reputation: 331
I want to load a list of products in view according to search. If user enters name or id the product should be shown. I tried but I'm only able to do with only one of these.
this is how i tried.
Controller
public class SearchController : Controller
{
private storeEntities db = new storeEntities();
public ActionResult Find()
{
var det = (from d in db.products
select d).ToList();
return View(det);
}
[HttpPost]
public ActionResult Find(int pid)
{
var det = (from d in db.products
where d.id == pid
select d).ToList();
return View(det);
}
}
View
@model IEnumerable<SearchAppMvc.Models.product>
@{
ViewBag.Title = "Find";
}
<h2>Find</h2>
<div>
@Html.Partial("_SearchPartial")
</div>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
category
</th>
<th>
productName
</th>
<th>
price
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.category)
</td>
<td>
@Html.DisplayFor(modelItem => item.productName)
</td>
<td>
@Html.DisplayFor(modelItem => item.price)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.id }) |
@Html.ActionLink("Details", "Details", new { id=item.id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.id })
</td>
</tr>
}
partial view
@using (Html.BeginForm( new RouteValueDictionary {{"controller","Search"},{"action","Find"},{"id","pid"}}))
{
@*@Html.Editor("name")*@
@Html.TextBox("pid")
<input type="submit" value="search"/>
}
and model is done using entity frame work
Upvotes: 0
Views: 17143
Reputation: 3479
In your Controller:
[HttpPost]
public ActionResult Find(string searchString)
{
var det = db.products.ToList();
if (!String.IsNullOrEmpty(searchString))
{
det = det.Where(d=>d.productname.ToUpper().Contains(searchString.ToUpper()) ||
d.productID == int.TryParse(searchString, out result))
}
return View(det)
}
In your View:
@using (Html.BeginForm())
{
<p>
@Html.TextBox("SearchString");
<input type="submit" value="Find"/>
</p>
}
Upvotes: 1
Reputation: 331
got solution
[HttpPost]
public ActionResult Find(string pid)
{
int x;
bool resl = int.TryParse(pid,out x);
if (resl ==true)
{
var det = (from d in db.products
where d.id == x
select d).ToList();
return View(det);
}
else
{
var det = (from d in db.products
where d.productName == pid
select d).ToList();
return View(det);
}
}
Upvotes: 0
Reputation: 3199
I would have a ProductSearchModel class like the following:
ProductSearchClass{
public int ProductId{get;set;}
public string ProductName{get;set;}
}
and Have the Controller action accept a copy of that model:
public ActionResult Find(ProductSearchModel criteria){
//Do the search logic here based on which property of the model is filled
//Return whatever view you want to return
}
Just make sure that in your view the ProductId and ProductName fields have the same name as the properties of the search model. The framework will then fill them for you based on which is filled in by the user.
Upvotes: 0
Reputation: 4059
[HttpPost]
public ActionResult Find(string pid)
{
var det = (from d in db.products
where d.id == Convert.Toint32(Pid) || d.name = pid
select d).ToList();
return View(det);
}
}
@using (Html.BeginForm("Results", "Search"))
{
@Html.TextBox("SearchText")
<input class="search-button1" type="submit" value="Search" />
}
Your Method accepts only one parameter here , try passing string to your method and in your query use OR operator to compare it with id or name . Hope it helps.
Upvotes: 0