由多个值搜索 [英] Search by multiple values

查看:114
本文介绍了由多个值搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

怎样才能使我的搜索引擎更接受输入不止一个这是由逗号分隔?

这是我的 index.view

 < P>
    @using(Html.BeginForm(指数,家,FormMethod.Get))
    {
    < B>搜索者:< / B> @ Html.RadioButton(搜索项,项ID,真)LT;文字>项目编号:LT; /文字和GT;
    < BR />
    @ Html.TextBox(搜索)<输入类型=提交值=搜索/>
    }
&所述; / P>
<表>
    &所述; TR>
        <第i个
            项目编号
        < /第i
        <第i个
            项目名
        < /第i
    < / TR>
    @if(Model.Count()== 0)
    {
        &所述; TR>
            < TD合并单元格=2>没有发现任何物品ID< / TD>
        < / TR>
    }
    其他
    {
        的foreach(在型号VAR项)
        {
            &所述; TR>
                &所述; TD>
                    @ Html.DisplayFor(modelItem => item.itemid)
                < / TD>
                &所述; TD>
                    @ Html.DisplayFor(modelItem => item.itemname)
                < / TD>
            < / TR>
        }
        <标题个总@ Html.En code(Model.Count())项目发现和LT; /字幕>
    }
< /表>

这是我的 homecontroller.cs

 使用系统;
使用System.Collections.Generic;
使用System.Data这;
使用System.Data.Entity的;
使用System.Linq的;
使用的System.Web;
使用System.Web.Mvc;
使用itemdb.Models;命名空间itemdb.Controllers
{
公共类HomeController的:控制器
{
    私人mydbEntities DB =新mydbEntities();    公众的ActionResult指数(字符串搜索项,字符串搜索)
    {
        如果(String.IsNullOrEmpty(搜索项)及!&放大器;!String.IsNullOrEmpty(搜索))
        {
            如果(搜索项==的itemid)
            {
                返回查看(db.itemlist.Where(X => x.items ==搜索).ToList());
            }
        其他
        {
            返回查看(db.itemlist.Take(0));
        }
    }
}
}


解决方案

 公众的ActionResult指数(字符串搜索项,字符串搜索)
    {
        如果(String.IsNullOrEmpty(搜索项)及!&放大器;!String.IsNullOrEmpty(搜索))
        {
            字符串[] =项目search.Split(新的char [] {','});
            如果(搜索项==的itemid)
            {
                返回查看(db.itemlist.Where(X => items.Contains(x.items))了ToList());
            }
        其他
        {
            返回查看(db.itemlist.Take(0));
        }
    }

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));
        }
    } 
}
}

解决方案

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));
        }
    } 

这篇关于由多个值搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆