public async Task <IActionResult> Edit(string name)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageTenantFeatureProfiles))
            {
                return(Forbid());
            }

            var featureProfilesDocument = await _featureProfilesManager.GetFeatureProfilesDocumentAsync();

            if (!featureProfilesDocument.FeatureProfiles.ContainsKey(name))
            {
                return(RedirectToAction(nameof(Create), new { name }));
            }

            var featureProfile = featureProfilesDocument.FeatureProfiles[name];

            var model = new FeatureProfileViewModel
            {
                Name         = name,
                FeatureRules = JsonConvert.SerializeObject(featureProfile.FeatureRules, Formatting.Indented)
            };

            return(View(model));
        }
        public async Task <IActionResult> Edit(string sourceName, FeatureProfileViewModel model, string submit)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageTenantFeatureProfiles))
            {
                return(Forbid());
            }

            var featureProfilesDocument = await _featureProfilesManager.LoadFeatureProfilesDocumentAsync();

            if (!featureProfilesDocument.FeatureProfiles.ContainsKey(sourceName))
            {
                return(NotFound());
            }

            List <FeatureRule> featureRules = null;

            if (ModelState.IsValid)
            {
                if (String.IsNullOrWhiteSpace(model.Name))
                {
                    ModelState.AddModelError(nameof(FeatureProfileViewModel.Name), S["The name is mandatory."]);
                }
                else if (!String.Equals(model.Name, sourceName, StringComparison.OrdinalIgnoreCase) &&
                         featureProfilesDocument.FeatureProfiles.ContainsKey(model.Name))
                {
                    ModelState.AddModelError(nameof(FeatureProfileViewModel.Name), S["A feature profile with the same name already exists."]);
                }

                if (String.IsNullOrEmpty(model.FeatureRules))
                {
                    ModelState.AddModelError(nameof(FeatureProfileViewModel.FeatureRules), S["The feature rules are mandatory."]);
                }
                else
                {
                    try
                    {
                        featureRules = JsonConvert.DeserializeObject <List <FeatureRule> >(model.FeatureRules);
                    }
                    catch (Exception)
                    {
                        ModelState.AddModelError(nameof(FeatureProfileViewModel.FeatureRules), S["Invalid json supplied."]);
                    }
                }
            }

            if (ModelState.IsValid)
            {
                var featureProfile = new FeatureProfile
                {
                    FeatureRules = featureRules
                };

                await _featureProfilesManager.RemoveFeatureProfileAsync(sourceName);

                await _featureProfilesManager.UpdateFeatureProfileAsync(model.Name, featureProfile);

                if (submit == "SaveAndContinue")
                {
                    return(RedirectToAction(nameof(Edit), new { name = model.Name }));
                }
                else
                {
                    return(RedirectToAction(nameof(Index)));
                }
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }