Canvas Search Field (ASP.Net - MVC)

Asked

Viewed 1,065 times

2

Good afternoon,

I wonder if someone could help me. I’m a beginner in programming, and I’m developing a Web system, and I need a screen search of the screens you’ve already created.

As I use the MVC method, I wanted to know if I have to create a View/Controller with the name Search.

**Remembering that I want to leave this on the home screen, and when searching, I wanted it to explode a box with the results, similar to the process that the site (https://trello.com) performs.

Thank you for reading so far!

Use VS2013,and the database use SQL Server.

1 answer

2

What I did in a system of my own was to create a Controller common implementation of the research method. Something like this:

public abstract class Controller<TSource> : System.Web.Mvc.Controller
    where TSource: class, IPesquisavel, new()
{
    protected MeuContext db = new MeuContext();

    public ActionResult Pesquisar(String termo)
    {
        var lista = db.Set<TSource>().Where(s => s.TermoPesquisa.Contains(termo)).ToList();
        return View(lista);
    }
}

To View you can put in Views/Shared (a little more complex to implement), or else make a View Pesquisar.cshtml for each Controller that has this research.

Implement IPesquisavel thus:

public interface IPesquisavel
{
    String TermoPesquisa { get; }
}

The Model that can receive this research gets like this:

public class MeuModel 
{
    ...
    public String Nome { get; set; }
    public String Descricao { get; set; }

    [DisplayName("Termo de Pesquisa")]
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public String TermoPesquisa
    {
        // No get, coloque os campos que interessam pra pesquisa, separados
        // por vírgula. Por exemplo, Nome e Descricao
        get { return Nome + ", " + Descricao; }
        private set { }
    }
}

By generating the Migration, Entity Framework does not understand that it is a calculated field. Comment on its generation and write in hand:

public partial class TestePesquisavel : DbMigration
{
    public override void Up()
    {
        // AddColumn("dbo.MeuModels", "TermoPesquisa", c => c.String());
        Sql("ALTER TABLE dbo.MeuModels ADD TermoPesquisa AS Nome + ', ' + Descricao");
    }

    public override void Down()
    {
        // DropColumn("dbo.MeuModels", "TermoPesquisa");
        Sql("ALTER TABLE dbo.MeuModels drop column TermoPesquisa");
    }
}
  • in my case, I have a View already on Shared called _Layout, where here is the system menu, and the search wanted to put it here too. In this case, a Controller would already solve?

  • You can do it. It’s going to get a little more complicated because you’re going to have to use Reflection to read the properties and write it right, but it works.

  • Suggest some other method for a layman and little experience?

  • This is the simplest method I know, which is to implement a Action common to all Controllers. The rest depart from unnecessary eccentricities.

  • Good morning @Gypsy, could you ask me one more question please? I am adapting the code to my system, what would be the "Meucontext"?

  • @Mariopueblajunior would be your data context. I’m guessing you’re using the Entity Framework.

Show 1 more comment

Browser other questions tagged

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