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.
What type of access you make to your bank, Sqlconnection or with Entity ?
– user6026
You can put in your question snippets of your already started code?
– Leonel Sanches da Silva
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 !
– Érik Thiago