create a search screen

4

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.

    
asked by anonymous 12.06.2014 / 01:22

1 answer

3

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 .

Controller

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

    ~HomeController()
    {
        db.Dispose();
    }

    [HttpGet]
    public ActionResult Pesquisa()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Pesquisa(string texto)
    {
        return View(db.Pessoas.Where(x => x.Nome.Contains(texto)).OrderBy(x => x.Nome));
    }
}

View:

@model IEnumerable<WebApp.Models.Pessoas>
@{ Layout = null; }
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Pesquisa</title>
</head>
<body>
    <div> 
         @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>   
         }
    </div>
    <table>
        <tr>
            <td>Id</td>
            <td>Nome</td>
        </tr>
        @{
            if (Model != null)
            {
                foreach (var item in Model)
                {
                    <tr>
                        <td>@item.PessoaId</td>
                        <td>@item.Nome</td>
                    </tr>
                }
            }
        }
    </table>
</body>
</html>

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.

    
12.06.2014 / 01:48