Doubt : Search with Dropdownlist mvc 4 Razor Asp.net C# sql Server

Asked

Viewed 355 times

1

I’m having difficulty in popular dropdown with sql server, because it comes repeated sql item ... ex: when I select cities it comes with repeated cities and so on, and I can’t direct to search page. follows the code

Controller Home:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        EDGSiteEntities op = new EDGSiteEntities();
        ViewBag.IdOperacao = new SelectList(op.Operacao, "IdOperacao", "Operacao1");
        EDGSiteEntities cid = new EDGSiteEntities();
        ViewBag.Cidade = new SelectList(cid.Cidade, "IdCidade", "Cidade1");

        return View(ResultadoBusca);
    }
    [HttpPost]
    public ActionResult Index(string IdOperacao,string Cidade)
    { 
    string operacao = IdOperacao;
    string operacaoRequest = Request["IdOperacao"];
    string cidade = Cidade;
    string cidadeRequest = Request["Cidade"];
    return View(ResultadoBusca);



    }

    private ActionResult View(Func<ActionResult> ResultadoBusca)
    {
        throw new NotImplementedException();

    }



    public ActionResult Institucional()
    {


        return View();
    }

    public ActionResult FaleConosco()
    {


        return View();
    }
    public ActionResult Home()
    {


        return View();
    }
    public ActionResult Imoveis()
    {


        return View();
    }
    public ActionResult Venda()
    {
        return View();
    }
    public ActionResult Locação()
    {
         return View();
    }
    public ActionResult Consorcio()
    {
        return View();
    }
    public ActionResult ResultadoBusca()
    {


        return View();
    }
    public ActionResult ImovelDetalhe()
    {


        return View();
    }
    public ActionResult Serviços()
    {


        return View();
    }
    public ActionResult Servicos_Locatario()
    {


        return View();
    }
    public ActionResult Servicos_Locador()
    {


        return View();
    }

index page

@{
    ViewBag.Title = "Home Page";
    Layout = "~/Views/Shared/_Layout.cshtml";

}

Quick search:

@using (Html.BeginForm())
{  
   @Html.DropDownList("IdOperacao","Selecione")
   @Html.DropDownList("Cidade", "Selecione")
   <input Type="submit" value="Pesquisar" />
}

what I’m looking to do and a search by city and type of home

  • What have you tried to do? Enter the code, it’s easier to help you.

  • Post your code! if you can table structure and your code in the controller!

  • okay just a moment

  • A question you are beginner in MVC Aspnet with Entity?

  • 1

    yes I’m a beginner in mvc but in aspx I know a little more

1 answer

4


It seems there are conceptual errors:

In the controller below:

public ActionResult Index()
{
        EDGSiteEntities op = new EDGSiteEntities();
        ViewBag.IdOperacao = new SelectList(op.Operacao, "IdOperacao", "Operacao1");
        EDGSiteEntities cid = new EDGSiteEntities();
        ViewBag.Cidade = new SelectList(cid.Cidade, "IdCidade", "Cidade1");

        return View(ResultadoBusca);
}

could be summarized to:

public ActionResult Index()
{
        EDGSiteEntities op = new EDGSiteEntities();
        ViewBag.IdOperacao = new SelectList(op.Operacao, "IdOperacao", "Operacao1");
        ViewBag.Cidade = new SelectList(op.Cidade.Distinct(), "IdCidade", "Cidade1");            

        return View(ResultadoBusca);
}

It had two instances of EDGSiteEntities, only needs one, and with it had all the information to generate the data for your View.In Cities could use the command Distinct() to not have repetition of information, but, I was in doubt, because in Brazil there are cities with the same name and different states, fit there a filter per state.

I don’t know if the code for you is all right, I made some changes, and now it’s testing in your code.

  • but and for sp region only, will I need to do a dll and bll to redirect to for products ?

  • No, not that the problem, post in your question the table City! and another the above code did not work?

  • 1

    yes, it did... obg @virgilio

Browser other questions tagged

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