create a search screen

Asked

Viewed 5,508 times

4

I am giving sequence in an application in ASP.Net MVC and need to make a search screen.

The question is this::

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 Search and the application should return all users who have the word "Stitch" within the table skills field.

How to implement this?

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

I would like on the controller to make a select ... where... like '%@PALAVRADIGITADA%' only I don’t know the syntax to pass this variable and such.

If anyone can give me a light, I’d appreciate it.

  • What type of access you make to your bank, Sqlconnection or with Entity ?

  • You can put in your question snippets of your already started code?

  • Cara I’ll put as a comment, but look at this link, which is from the official site of Asp.net mvc, or even Asp.net as well. This link has a very easy search to do, and gives a very cool north and this way you can move forward ! Follow the link. Follow this example from @Harrypotter also to get another notion of how to do !

1 answer

3

Example with Entity Framework

Create a Controller and put two methods by the name of Research. The method Research decorated with HttpGet will respond to requests Get and the decorated with HttpPost will answer for the requisitions Post. When clicking the button goes to the method Research decorated with HttpPost, that has a text parameter that is sent from the form Html.

Controler

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 the Filter button it goes to the method Question with Paramento text and make a filter (SQL like by the name of the entity Persons) and send the result to the same screen.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.