create a search screen

Posted on

Question :

I’m following up on an ASP.Net MVC application and I need to do a search screen.

The question is as follows:

I created a table where users register their skills.

I need a view where there will be a text field where the person will type for example: “Sewing”. Click fetch and the application should return all users who have the word “Sewing” inside the table skills field.

How to implement this?

It’s like these search fields that we have on every site. I think it’s pretty simple, but I do not know where to start.

I would like in the controller to make a select ... where... like '%@PALAVRADIGITADA%' only that I do not know the syntax to pass this variable and such.

If anyone can give me a light, thank you in advance.


Answer :

Example with Entity Framework

Create a Controller and put two methods with the name of Search . The Search method decorated with HttpGet will respond to requests Get and the set with HttpPost will respond to requests Post . How to click the button goes to the Search method decorated with HttpPost , which has a param paramenter text that is sent from the form Html .


public class HomeController : Controller
    private readonly ModelDb db;
    public HomeController()
        db = new ModelDb();


    public ActionResult Pesquisa()
        return View();
    public ActionResult Pesquisa(string texto)
        return View(db.Pessoas.Where(x => x.Nome.Contains(texto)).OrderBy(x => x.Nome));


@model IEnumerable<WebApp.Models.Pessoas>
@{ Layout = null; }
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width" />
         @using (Html.BeginForm())
             <p>Digite o nome</p>
             <input type="text" name="texto" id="texto" placeholder="Digite a pesquisa" />
             <div><button type="submit">Filtrar</button></div>   
            if (Model != null)
                foreach (var item in Model)

By clicking on the Filter button it goes to the Question method with text and will filter ( SQL like >) and will send the result to the same screen.


Leave a Reply

Your email address will not be published. Required fields are marked *