//
        // GET: /Right/Add
        public ActionResult Add()
        {
            var applicationList = SelectListItemExtension.PopulateDropdownList(_applicationRepository.GetAll().ToList<TblApplication>(), "ApplicationId", "ApplicationName").ToList();

            var moduleList = SelectListItemExtension.PopulateDropdownList(_moduleRepository.GetAll().ToList<TblModule>(), "ModuleId", "ModuleTitle").ToList();

            var viewModel = new RightViewModel() { RightId = 0, ddlApplications = applicationList, ddlModules = moduleList };
            //return View();
            return PartialView("_AddOrEdit", viewModel);
        }
        //
        // GET: /Right/Details/By ID
        public ActionResult Details(int id)
        {
            var errorViewModel = new ErrorViewModel();

            try
            {
                //var right = _rightRepository.GetById(id);
                var right = _rightRepository.GetAll().SingleOrDefault(x => x.RightId == id);
                if (right != null)
                {

                    var singleOrDefaultApplication = _applicationRepository.GetAll().SingleOrDefault(x => x.ApplicationId == right.ApplicationId);

                    var singleOrDefaultModule = _moduleRepository.GetAll().SingleOrDefault(x => x.ModuleId == right.ModuleId);

                    if (singleOrDefaultApplication != null && singleOrDefaultModule != null)
                    {
                        var viewModel = new RightViewModel() { RightId = right.RightId, RightName = right.RightName, RightTitle = right.RightTitle, Description = right.Description, ApplicationId = Convert.ToInt32(right.ApplicationId), ApplicationName = singleOrDefaultApplication.ApplicationName, ModuleId = Convert.ToInt32(right.ModuleId), ModuleName = singleOrDefaultModule.ModuleName };

                        return PartialView("_Details", viewModel);
                    }
                }

                errorViewModel = ExceptionHelper.ExceptionErrorMessageForNullObject();
            }
            catch (Exception ex)
            {
                errorViewModel = ExceptionHelper.ExceptionErrorMessageFormat(ex);
            }

            return PartialView("_ErrorPopup", errorViewModel);
        }
        public ActionResult Save(RightViewModel rightViewModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //add
                    if (rightViewModel.RightId == 0 && rightViewModel.ActionName == "Add")
                    {
                        var model = new TblRight()
                        {
                            RightId = rightViewModel.RightId,
                            RightName = rightViewModel.RightName,
                            RightTitle = rightViewModel.RightTitle,
                            Description = rightViewModel.Description,
                            ApplicationId = rightViewModel.ApplicationId,
                            ModuleId = rightViewModel.ModuleId
                        };

                        _rightRepository.Insert(model);
                    }
                    else if (rightViewModel.ActionName == "Edit") //edit
                    {
                        TblRight right = _rightRepository.GetById(rightViewModel.RightId);

                        if (right != null)
                        {

                            right.RightId = rightViewModel.RightId;
                            right.RightName = rightViewModel.RightName;
                            right.RightTitle = rightViewModel.RightTitle;
                            right.Description = rightViewModel.Description;

                            right.ApplicationId = rightViewModel.ApplicationId;
                            right.ModuleId = rightViewModel.ModuleId;

                            _rightRepository.Update(right);

                        }
                        else
                        {
                            return Content(KendoUiHelper.GetKendoUiWindowAjaxSuccessMethod(Boolean.FalseString, rightViewModel.ActionName, MessageType.warning.ToString(), ExceptionHelper.ExceptionMessageForNullObject()));
                        }

                    }

                    _rightRepository.Save();

                    return Content(KendoUiHelper.GetKendoUiWindowAjaxSuccessMethod(Boolean.TrueString, rightViewModel.ActionName, MessageType.success.ToString(), "Saved Successfully."));

                }

                return Content(KendoUiHelper.GetKendoUiWindowAjaxSuccessMethod(Boolean.FalseString, rightViewModel.ActionName, MessageType.warning.ToString(), ExceptionHelper.ModelStateErrorFormat(ModelState)));
            }
            catch (Exception ex)
            {
                return Content(KendoUiHelper.GetKendoUiWindowAjaxSuccessMethod(Boolean.FalseString, rightViewModel.ActionName, MessageType.warning.ToString(), ExceptionHelper.ExceptionMessageFormat(ex)));
            }
        }
        //
        // GET: /Right/Edit/By ID
        public ActionResult Edit(int id)
        {
            var errorViewModel = new ErrorViewModel();

            try
            {
                //var right = _rightRepository.GetById(id);
                var right = _rightRepository.GetAll().SingleOrDefault(x => x.RightId == id);
                if (right != null)
                {

                    var moduleList = SelectListItemExtension.PopulateDropdownList(_moduleRepository.GetAll().ToList<TblModule>(), "ModuleId", "ModuleName", isEdit: true, selectedValue: right.ModuleId.ToString()).ToList();

                    var applicationList = SelectListItemExtension.PopulateDropdownList(_applicationRepository.GetAll().ToList<TblApplication>(), "ApplicationId", "ApplicationName", isEdit: true, selectedValue: right.ApplicationId.ToString()).ToList();

                    var singleOrDefaultApplication = _applicationRepository.GetAll().SingleOrDefault(x => x.ApplicationId == right.ApplicationId);

                    var singleOrDefaultModule = _moduleRepository.GetAll().SingleOrDefault(x => x.ModuleId == right.ModuleId);

                    if (singleOrDefaultApplication != null && singleOrDefaultModule != null)
                    {

                        var viewModel = new RightViewModel()
                        {
                            RightId = right.RightId,
                            RightName = right.RightName,
                            RightTitle = right.RightTitle,
                            Description = right.Description,
                            ApplicationId = Convert.ToInt32(right.ApplicationId),
                            ApplicationName = singleOrDefaultApplication != null ? singleOrDefaultApplication.ApplicationName : null,
                            ddlApplications = applicationList,
                            ModuleId = Convert.ToInt32(right.ModuleId),
                            ModuleName = singleOrDefaultModule != null ? singleOrDefaultModule.ModuleName : null,
                            ddlModules = moduleList
                        };
                        return PartialView("_AddOrEdit", viewModel);
                    }
                }

                errorViewModel = ExceptionHelper.ExceptionErrorMessageForNullObject();
            }
            catch (Exception ex)
            {
                errorViewModel = ExceptionHelper.ExceptionErrorMessageFormat(ex);
            }

            return PartialView("_ErrorPopup", errorViewModel);
        }