public JsonResult AjaxExcluir(string idservico, string idperfil)
 {
     try
     {
         var p = new Perfil()
         {
             TipoAcao = 3, //TipoAcao 3 é Exclusão
             idPerfil = int.Parse(idperfil),
             idServico = int.Parse(idservico)
         };
         var Ret = new PerfilRepository();
         var Retorno = Ret.Excluir(p);
         if (Retorno.CodigoRetorno < 0)
         {
             throw new Exception(Retorno.Mensagem);
         }
         //ViewBag.Msg = Retorno.Mensagem;
         return Json(Retorno, JsonRequestBehavior.AllowGet);
     }
     catch (Exception ex)
     {
         TempData["Error"] = ex.Message;
         //throw new Exception(ex.Message);
         return Json(ex.Message, JsonRequestBehavior.AllowGet);
     }
 }
        public Retorno Incluir(Perfil p)
        {
            try
            {
                DbCommand _cmd;
                Database _db = DbConn.CreateDB();
                _cmd = _db.GetStoredProcCommand("proc_Manutencao_Perfil");
                _db.AddInParameter(_cmd, "@TipoAcao", DbType.Int16, p.TipoAcao);
                _db.AddInParameter(_cmd, "@Descricao", DbType.String, p.Descricao);
                _db.AddInParameter(_cmd, "@idServico", DbType.Int16, p.idServico);
                _db.AddInParameter(_cmd, "@idPerfil", DbType.Int16, p.idPerfil);
                _db.AddInParameter(_cmd, "@idModulo", DbType.String, p.idModulo);
                _db.AddInParameter(_cmd, "@QtdeModulo", DbType.Int16, p.qtdeModulo);

                var _Ret = new Retorno();

                using (IDataReader _dr = _db.ExecuteReader(_cmd))
                {
                    while (_dr.Read())
                    {
                        _Ret.CodigoRetorno = int.Parse(_dr[0].ToString());
                        _Ret.Mensagem = _dr[1].ToString();
                    }
                }
                return _Ret;
            }
            catch (Exception ex) { throw ex; }
        }
        public ActionResult Create(FormCollection frm)
        {
            var Retorno = new Retorno();
            try
            {

                int idperfil = int.Parse(frm["acao"].ToString()) == 1 ? 0 : int.Parse(frm["SelPerfil"].ToString());
                var p = new Perfil()
                {
                     TipoAcao = int.Parse(frm["acao"].ToString()),
                     Descricao = idperfil == 0 ? frm["SelPerfil"].ToString() : frm["nomeperfil"].ToString(),
                     idServico = int.Parse(frm["SelServico"].ToString()),
                     idPerfil = idperfil>0?idperfil:0,
                     idModulo = frm["modulos"].ToString(),
                     qtdeModulo = int.Parse(frm["qtdeModulos"].ToString()),
                };

                var perfilRet = new PerfilRepository();
                Retorno = perfilRet.Incluir(p);
                if (Retorno.CodigoRetorno < 0)
                {
                    throw new Exception(Retorno.Mensagem);
                }
                TempData["Msg"] = Retorno.Mensagem;
                return RedirectToAction("Index");

            }
            catch(Exception ex)
            {
                TempData["Error"] = ex.Message;
                return View();
            }
        }