コード例 #1
0
        public async Task <IActionResult> Add(AddingEditingItemViewModel model)
        {
            model.ReturnUrl ??= "~/";
            model.Collection = collectionManager.GetById(model.CollectionId, true);
            if (model.Collection == null)
            {
                return(View("Error", new ErrorViewModel
                {
                    ErrorTitle = localizer["CollectionNotFoundTitle"],
                    ErrorMessage = localizer["CollectionNotFoundMessage"]
                }));
            }

            var view = await CheckUser(model.Collection.User.UserName);

            if (view != null)
            {
                return(view);
            }

            if (!ValidateFields(model))
            {
                return(View("Error", new ErrorViewModel
                {
                    ErrorTitle = localizer["ValidationErrorMessage"],
                    ErrorMessage = localizer["ValidationErrorTitle", model.Collection.Name],
                    ButtonLabel = localizer["ToCollectionPage"],
                    Url = model.ReturnUrl
                }));
            }
            if (ModelState.IsValid)
            {
                var item = new Item
                {
                    Name         = model.Name,
                    CollectionId = model.CollectionId,
                    FieldValues  = GetFieldValues(model)
                };
                if (await itemManager.AddAsync(item) == OperationResult.Failed)
                {
                    return(View("Error", new ErrorViewModel
                    {
                        ErrorTitle = localizer["AddingItemErrorTitle"],
                        ErrorMessage = localizer["AddingItemErrorMessage"],
                        ButtonLabel = localizer["ToCollectionPage"],
                        Url = model.ReturnUrl
                    }));
                }
                await AddTagsAsync(item.Id, model);

                return(Redirect(model.ReturnUrl));
            }
            return(View("AddEdit", model));
        }