public JsonResult ObterEndereco(string cep)
        {
            ViewBag.ClienteId = clienteRepository.ObterTodos();

            string url = ConfigurationManager.AppSettings["WsCEP"].Replace("xxxxxxxx", cep);

            string         conteudo = "";
            Task <string>  retorno;
            EnderecoViaCep enderecoViaCep;
            EnderecoSet    endereco;

            ServicoHttp servicoHttp = new ServicoHttp();

            retorno = servicoHttp.Post(url, conteudo);

            enderecoViaCep = JsonConvert.DeserializeObject <EnderecoViaCep>(retorno.Result);

            if (enderecoViaCep.erro)
            {
                return(Json(new { erro = true }, JsonRequestBehavior.AllowGet));
            }

            endereco = new EnderecoSet
            {
                Bairro      = enderecoViaCep.bairro,
                CEP         = Int32.Parse(enderecoViaCep.cep.Replace("-", "")),
                Logradouro  = enderecoViaCep.logradouro,
                Complemento = enderecoViaCep.complemento,
                Estado      = enderecoViaCep.uf,
                Municipio   = enderecoViaCep.localidade,
            };

            return(Json(endereco, JsonRequestBehavior.AllowGet));
        }
        public ActionResult DeleteConfirmed(int id)
        {
            EnderecoSet enderecoSet = enderecoRepository.ObterPorId(id);

            enderecoRepository.Apagar(enderecoSet);
            return(RedirectToAction("Index"));
        }
 public ActionResult Edit([Bind(Include = "Id,ClienteId,Logradouro,Numero,Complemento,CEP,Bairro,Municipio,Estado")] EnderecoSet enderecoSet)
 {
     if (ModelState.IsValid)
     {
         enderecoRepository.Atualizar(enderecoSet);
         return(RedirectToAction("Index"));
     }
     return(View(enderecoSet));
 }
        // GET: Endereco/Delete/5
        public ActionResult Delete(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            EnderecoSet enderecoSet = enderecoRepository.ObterPorId(id.Value);

            if (enderecoSet == null)
            {
                return(HttpNotFound());
            }
            return(View(enderecoSet));
        }
 public void Inserir(EnderecoSet entity)
 {
     try
     {
         if (entity != null)
         {
             dbcontext.EnderecoSet.Add(entity);
             dbcontext.Entry(entity).State = System.Data.Entity.EntityState.Added;
             dbcontext.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         logger.Error(ex);
     }
 }
 public void Apagar(EnderecoSet entity)
 {
     try
     {
         if (entity != null)
         {
             dbcontext.EnderecoSet.Remove(entity);
             dbcontext.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
             dbcontext.SaveChanges();
         }
     }
     catch (Exception ex)
     {
         logger.Error(ex);
     }
 }
        public ActionResult Create([Bind(Include = "Id,ClienteId,Logradouro,Numero,Complemento,CEP,Bairro,Municipio,Estado")] EnderecoSet enderecoSet)
        {
            if (enderecoSet.ClienteId <= 0)
            {
                ModelState.AddModelError("ClienteId", "O campo ClienteId é obrigatório.");
            }

            if (ModelState.IsValid)
            {
                enderecoRepository.Inserir(enderecoSet);
                return(RedirectToAction("Index"));
            }

            ViewBag.ClienteId = clienteRepository.ObterTodos();
            return(View(enderecoSet));
        }