2
I asked a question with another user but I do not know what happened that I could not recover the information and I had to create another one now. Anyway, I wanted to create a search field and I was given as response to controller and view 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! Only now, I need to do the same thing but in the view I have to return information from two different tables. What’s the syntax for a controller Join or how do I call it in the view since I can’t 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; }
}
Pass your template on the question please and it was I who answered the previous
– user6026
You cannot pass two models in the same view, you must use Viewbag or Viewdata !!!
– user6026
That! That’s right! Thank you so much for your help! That’s just what I needed! Only that I need at the time of return along with the text field, return the information Plate and Name of another table on the grid! How do I do this?
– PFVictor