Esempio n. 1
0
    /// <summary>
    ///     Creates the model from the request and binds it to the context
    /// </summary>
    /// <param name="bindingContext"></param>
    /// <returns></returns>
    public async Task BindModelAsync(ModelBindingContext bindingContext)
    {
        MediaItemSave?model =
            await _modelBinderHelper.BindModelFromMultipartRequestAsync <MediaItemSave>(_jsonSerializer, _hostingEnvironment, bindingContext);

        if (model == null)
        {
            return;
        }

        model.PersistedContent = ContentControllerBase.IsCreatingAction(model.Action)
            ? CreateNew(model)
            : GetExisting(model) !;

        //create the dto from the persisted model
        if (model.PersistedContent != null)
        {
            model.PropertyCollectionDto =
                _umbracoMapper.Map <IMedia, ContentPropertyCollectionDto>(model.PersistedContent);
            //now map all of the saved values to the dto
            _modelBinderHelper.MapPropertyValuesFromSaved(model, model.PropertyCollectionDto);
        }

        model.Name = model.Name?.Trim();

        bindingContext.Result = ModelBindingResult.Success(model);
    }
Esempio n. 2
0
        public override ActionResult Index()
        {
            AssertProductsLoaded();
            ContentControllerBase <PageModelBase> .SetPageTitleSimple(CurrentItem, ViewBag);

            return(base.Index());
        }
        /// <summary>
        /// Creates the model from the request and binds it to the context
        /// </summary>
        /// <param name="actionContext"></param>
        /// <param name="bindingContext"></param>
        /// <returns></returns>
        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            var model = _modelBinderHelper.BindModelFromMultipartRequest <ContentItemSave>(actionContext, bindingContext);

            if (model == null)
            {
                return(false);
            }

            model.PersistedContent = ContentControllerBase.IsCreatingAction(model.Action) ? CreateNew(model) : GetExisting(model);

            //create the dto from the persisted model
            if (model.PersistedContent != null)
            {
                foreach (var variant in model.Variants)
                {
                    //map the property dto collection with the culture of the current variant
                    variant.PropertyCollectionDto = Current.Mapper.Map <ContentPropertyCollectionDto>(
                        model.PersistedContent,
                        context =>
                    {
                        // either of these may be null and that is ok, if it's invariant they will be null which is what is expected
                        context.SetCulture(variant.Culture);
                        context.SetSegment(variant.Segment);
                    });

                    //now map all of the saved values to the dto
                    _modelBinderHelper.MapPropertyValuesFromSaved(variant, variant.PropertyCollectionDto);
                }
            }

            return(true);
        }
Esempio n. 4
0
        /// <summary>
        /// Creates the model from the request and binds it to the context
        /// </summary>
        /// <param name="actionContext"></param>
        /// <param name="bindingContext"></param>
        /// <returns></returns>
        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            var model = ContentModelBinderHelper.BindModelFromMultipartRequest <ContentItemSave>(actionContext, bindingContext);

            if (model == null)
            {
                return(false);
            }

            BindModel(model, ContentControllerBase.IsCreatingAction(model.Action) ? CreateNew(model) : GetExisting(model));

            return(true);
        }
        public async Task BindModelAsync(ModelBindingContext bindingContext)
        {
            if (bindingContext == null)
            {
                throw new ArgumentNullException(nameof(bindingContext));
            }

            var model = await _modelBinderHelper.BindModelFromMultipartRequestAsync <ContentItemSave>(_jsonSerializer, _hostingEnvironment, bindingContext);

            if (model is null)
            {
                return;
            }

            var persistedContent = ContentControllerBase.IsCreatingAction(model.Action) ? CreateNew(model) : GetExisting(model);

            BindModel(model, persistedContent, _modelBinderHelper, _umbracoMapper);

            bindingContext.Result = ModelBindingResult.Success(model);
        }
Esempio n. 6
0
        /// <summary>
        /// Creates the model from the request and binds it to the context
        /// </summary>
        /// <param name="actionContext"></param>
        /// <param name="bindingContext"></param>
        /// <returns></returns>
        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            var model = _modelBinderHelper.BindModelFromMultipartRequest <MemberSave>(actionContext, bindingContext);

            if (model == null)
            {
                return(false);
            }

            model.PersistedContent = ContentControllerBase.IsCreatingAction(model.Action) ? CreateNew(model) : GetExisting(model);

            //create the dto from the persisted model
            if (model.PersistedContent != null)
            {
                model.PropertyCollectionDto = Current.Mapper.Map <IMember, ContentPropertyCollectionDto>(model.PersistedContent);
                //now map all of the saved values to the dto
                _modelBinderHelper.MapPropertyValuesFromSaved(model, model.PropertyCollectionDto);
            }

            model.Name = model.Name.Trim();

            return(true);
        }
Esempio n. 7
0
        /// <summary>
        /// Creates the model from the request and binds it to the context
        /// </summary>
        /// <param name="actionContext"></param>
        /// <param name="bindingContext"></param>
        /// <returns></returns>
        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            var model = _modelBinderHelper.BindModelFromMultipartRequest <ContentItemSave>(actionContext, bindingContext);

            if (model == null)
            {
                return(false);
            }

            model.PersistedContent = ContentControllerBase.IsCreatingAction(model.Action) ? CreateNew(model) : GetExisting(model);

            //create the dto from the persisted model
            if (model.PersistedContent != null)
            {
                foreach (var variant in model.Variants)
                {
                    if (variant.Culture.IsNullOrWhiteSpace())
                    {
                        //map the property dto collection (no culture is passed to the mapping context so it will be invariant)
                        variant.PropertyCollectionDto = Mapper.Map <ContentPropertyCollectionDto>(model.PersistedContent);
                    }
                    else
                    {
                        //map the property dto collection with the culture of the current variant
                        variant.PropertyCollectionDto = Mapper.Map <ContentPropertyCollectionDto>(
                            model.PersistedContent,
                            options => options.SetCulture(variant.Culture));
                    }

                    //now map all of the saved values to the dto
                    _modelBinderHelper.MapPropertyValuesFromSaved(variant, variant.PropertyCollectionDto);
                }
            }

            return(true);
        }
        /// <summary>
        /// Builds the model from the request contents
        /// </summary>
        /// <param name="actionContext"></param>
        /// <param name="bindingContext"></param>
        /// <param name="provider"></param>
        /// <returns></returns>
        private async Task <TModelSave> GetModelAsync(HttpActionContext actionContext, ModelBindingContext bindingContext, MultipartFormDataStreamProvider provider)
        {
            var request = actionContext.Request;

            //IMPORTANT!!! We need to ensure the umbraco context here because this is running in an async thread
            var httpContext = (HttpContextBase)request.Properties["MS_HttpContext"];

            UmbracoContext.EnsureContext(
                httpContext,
                ApplicationContext.Current,
                new WebSecurity(httpContext, ApplicationContext.Current));

            var content = request.Content;

            var result = await content.ReadAsMultipartAsync(provider);

            if (result.FormData["contentItem"] == null)
            {
                var response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest);
                response.ReasonPhrase = "The request was not formatted correctly and is missing the 'contentItem' parameter";
                throw new HttpResponseException(response);
            }

            //get the string json from the request
            var contentItem = result.FormData["contentItem"];

            //deserialize into our model
            var model = JsonConvert.DeserializeObject <TModelSave>(contentItem);

            //get the default body validator and validate the object
            var bodyValidator    = actionContext.ControllerContext.Configuration.Services.GetBodyModelValidator();
            var metadataProvider = actionContext.ControllerContext.Configuration.Services.GetModelMetadataProvider();

            //all validation errors will not contain a prefix
            bodyValidator.Validate(model, typeof(TModelSave), metadataProvider, actionContext, "");

            //get the files
            foreach (var file in result.FileData)
            {
                //The name that has been assigned in JS has 2 parts and the second part indicates the property id
                // for which the file belongs.
                var parts = file.Headers.ContentDisposition.Name.Trim(new char[] { '\"' }).Split('_');
                if (parts.Length != 2)
                {
                    var response = actionContext.Request.CreateResponse(HttpStatusCode.BadRequest);
                    response.ReasonPhrase = "The request was not formatted correctly the file name's must be underscore delimited";
                    throw new HttpResponseException(response);
                }
                var propAlias = parts[1];

                var fileName = file.Headers.ContentDisposition.FileName.Trim(new char[] { '\"' });

                model.UploadedFiles.Add(new ContentItemFile
                {
                    TempFilePath  = file.LocalFileName,
                    PropertyAlias = propAlias,
                    FileName      = fileName
                });
            }

            if (ContentControllerBase.IsCreatingAction(model.Action))
            {
                //we are creating new content
                model.PersistedContent = CreateNew(model);
            }
            else
            {
                //finally, let's lookup the real content item and create the DTO item
                model.PersistedContent = GetExisting(model);
            }

            //create the dto from the persisted model
            if (model.PersistedContent != null)
            {
                model.ContentDto = MapFromPersisted(model);
            }
            if (model.ContentDto != null)
            {
                //now map all of the saved values to the dto
                MapPropertyValuesFromSaved(model, model.ContentDto);
            }

            model.Name = model.Name.Trim();

            return(model);
        }