I asked another user but I do not know what happened that I could not recover the information and had to create another one now. Anyway, I wanted to create a search field and gave me the controller and view answer below:
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>
Great, that's exactly what I needed, Harry Polter! But now, I need to do the same thing but in the view I have to return information from two different tables. What is the syntax to do a join in the controller or how do I call this in the view since I can not put two models as:
@model IEnumerable<WebApp.Models.Pessoas>
@model IEnumerable<WebApp.Models.Informacoes>
?
The models
namespace Competências.Models
{
public class Informacoes
{
public int Id { get; set; }
[Required(ErrorMessage = "A Chapa é obrigatória.")]
[StringLength(16, ErrorMessage = "A Chapa pode ter no máximo 16 caracteres.")]
public string Chapa { get; set; }
[Required(ErrorMessage = "O Nome é obrigatório.")]
[StringLength(120, ErrorMessage = "O Nome pode ter no máximo 120 caracteres.")]
public string Nome { get; set; }
}
public class Pessoas
{
public int Id { get; set; }
[Required(ErrorMessage = "É obrigatório descrever as atividades desempenhadas na empresa")]
[StringLength(255, ErrorMessage = "O campo atividades pode ter no máximo 255 caracteres")]
public string Atividades { get; set; }
public int InformacoesId { get; set; }
public virtual Informacoes Informacoes { get; set; }
}