Question: Search with DropDownlist mvc 4 razor asp.net C # sql Server [closed]

1

I am having difficulty popping the dropdown with sql server, because it comes repeated sql item ... ex: when I select the cities it comes with repeated cities and so on, and I can not direct to the search page. follow 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();
    }

page index

@{
    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 trying to do and a search for city and house type

    
asked by anonymous 12.09.2016 / 21:09

1 answer

4

It looks like it has conceptual errors:

No 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 summed up for:

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 , it only needed one, and with it it had all the information to generate the data for its View . In Cities could use the Distinct() command to not have repetition of information, but , I stayed in doubt, because in Brazil there are cities with the same name and different states, it fits one filter per state.

I do not know if the code for you is all correct, I made some changes, and now it's testing in your code.

    
12.09.2016 / 21:32