Querying using linq to sql in ASP.NET MVC

1

I have a question about how to return the records according to the query. I do not know what might be wrong, but the query is not returning according to what was passed.

I have my controller:

public ActionResult Index()
{
    //retornar todos os registros  
    sistema_mobileEntities dao = new sistema_mobileEntities();
    TempData["titulo1"] = "Consulta Registro";
    return View(dao.cliente.ToList());

}

[HttpPost]
public ActionResult Index(string recebeNome, int recebeOpcao)
{

    try
    {
        sistema_mobileEntities dao = new sistema_mobileEntities();

        if (recebeOpcao == 1)
        {
            var sql = from c in dao.cliente
                    where SqlMethods.Like(c.nome, recebeNome.Trim() + "%")
                    select c;

            TempData["opcao1"] = "nome";
        }

        if (recebeOpcao == 2)
        {
            var sql = from c in dao.cliente
                      where SqlMethods.Like(c.pai, recebeNome.Trim() + "%")
                      select c;

            TempData["opcao2"] = "pai";
        }

        if (recebeOpcao == 3)
        {
            var sql = from c in dao.cliente
                      where SqlMethods.Like(c.mae, recebeNome.Trim() + "%")
                      select c;

            TempData["opcao3"] = "mae";
        }

        return View(dao.cliente.ToList());
    }
    catch (Exception ex)
    {
        return Json("Erro ao consultar usuario" + ex.Message);
    }


}
    
asked by anonymous 05.04.2015 / 15:27

1 answer

3

You are not returning the variable that was used in the search.

The line:

return View(dao.cliente.ToList());

Should be:

return View(sql.ToList());
    
06.04.2015 / 13:13