join for search field

2

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; }

}
    
asked by anonymous 12.06.2014 / 17:34

1 answer

1

To pass Controller information to View use ViewBag or ViewData .

Example:

Note that ViewBag.Flux will be the transport for your View, it can be used normally for various data types.

public ActionResult Pesquisa(string texto)
{
        ViewBag.Fluxo = db.Fluxo.AsEnumerable();
        return View(db.Pessoas.Where(x => x.Nome.Contains(texto)).OrderBy(x => x.Nome));
}

View:

@model IEnumerable<WebApp.Models.Pessoas>
@{ Layout = null; }
@{    
    IEnumerable<WebApp.Models.Fluxo> modelFluxo = (IEnumerable<WebApp.Models.Fluxo>)ViewBag.Fluxo;
}

Another way would be with a Model class

Example:

public class Models
{
    public IList Model { get; set; }
    public Models()
    {
        Model = new List<Object>();
    }
    public void Add(Object model)
    {
        this.Model.Add(model);
    }
}

Controller

public ActionResult Pesquisa(string texto)
{
    Models.Models model = new Models.Models();
    model.Add(db.Fluxo);
    model.Add(db.Pessoas.Where(x => x.Nome.Contains(texto)).OrderBy(x => x.Nome));
    return View(model);
}

View:

@model WebApp.Models.Models
@{ Layout = null; }
@{  
    IEnumerable<WebApp.Models.Fluxo> modelFluxo = null;
    IEnumerable<WebApp.Models.Pessoas> modelPessoa = null;
    if (Model != null) { 
        modelFluxo = Model.Model[0] as IEnumerable<WebApp.Models.Fluxo>;
        modelPessoa = Model.Model[1] as IEnumerable<WebApp.Models.Pessoas>;
    }
}
    
12.06.2014 / 18:20