Reputation: 676
How can i make my search engine accept inputs more than one which are seperated by comma?
This is my index.view:
<p>
@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
<b>Search by:</b> @Html.RadioButton("searchby", "ItemID", true)<text>Item Id</text>
<br />
@Html.TextBox("search") <input type="submit" value="Search" />
}
</p>
<table>
<tr>
<th>
ItemID
</th>
<th>
Item name
</th>
</tr>
@if (Model.Count() == 0)
{
<tr>
<td colspan="2">no item id found.</td>
</tr>
}
else
{
foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.itemid)
</td>
<td>
@Html.DisplayFor(modelItem => item.itemname)
</td>
</tr>
}
<caption>Total @Html.Encode(Model.Count()) item found.</caption>
}
</table>
And this is my homecontroller.cs:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using itemdb.Models;
namespace itemdb.Controllers
{
public class HomeController : Controller
{
private mydbEntities db = new mydbEntities();
public ActionResult Index(string searchBy, string search)
{
if (!String.IsNullOrEmpty(searchBy) && !String.IsNullOrEmpty(search))
{
if (searchBy == "itemid")
{
return View(db.itemlist.Where(x => x.items == search).ToList());
}
else
{
return View(db.itemlist.Take(0));
}
}
}
}
Upvotes: 0
Views: 971
Reputation: 29694
public ActionResult Index(string searchBy, string search)
{
if (!String.IsNullOrEmpty(searchBy) && !String.IsNullOrEmpty(search))
{
string[] items = search.Split(new Char[] {','});
if (searchBy == "itemid")
{
return View(db.itemlist.Where(x => items.Contains(x.items)).ToList());
}
else
{
return View(db.itemlist.Take(0));
}
}
Upvotes: 3