How to implement search functionality in C#/ASP.NET MVC

前端 未结 7 2035
执笔经年
执笔经年 2021-02-02 01:54

I am developing an ASP.NET MVC 3 application using C# and Razor.

I have a search form that looks like this: \"searc

7条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-02 02:06

    Not sure if you are using MS SQL. Seems SQL could do most of the work for you, and you can build dynamic queries. Obviously the select/from statement needs work, but you can get the idea from the where clause.

    DECLARE @SEARCHTYPE VARCHAR(20)
    DECLARE @SEARCHTERM VARCHAR(100)
    
    SELECT
        [FIELDS]
    FROM
        [TABLE]
    WHERE
        (@SEARCHTYPE = 'BEGINSWITH' AND [FIELD] LIKE @SEARCHTERM + '%') OR
        (@SEARCHTYPE = 'ENDSWITH' AND [FIELD] LIKE '%' + @SEARCHTERM) OR
        (@SEARCHTYPE = 'EQUALS' AND [FIELD] = @SEARCHTERM)
    

提交回复
热议问题