Join for field search

Asked

Viewed 240 times

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

  • You cannot pass two models in the same view, you must use Viewbag or Viewdata !!!

  • 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?

1 answer

1

To pass on information from Controller for View use Viewbag or Viewdata.

Example:

Realize that Viewbag.Flow will be the transport to your View, can be used normally for various types of data.

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

    Ball show! I just needed to add a "Multipleactiveresultsets=true;" to my string Connection and it worked beautifully! It helped me so much!!!

Browser other questions tagged

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