如何让我的搜索引擎接受多个以逗号分隔的输入?
这是我的 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>
这是我的 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));
}
}
}
}
答案 0 :(得分:3)
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));
}
}