示例#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);
    }
示例#2
0
    internal static void BindModel(ContentItemSave model, IContent persistedContent,
                                   ContentModelBinderHelper modelBinderHelper, IUmbracoMapper umbracoMapper)
    {
        model.PersistedContent = persistedContent;

        //create the dto from the persisted model
        if (model.PersistedContent != null)
        {
            foreach (ContentVariantSave variant in model.Variants)
            {
                //map the property dto collection with the culture of the current variant
                variant.PropertyCollectionDto = umbracoMapper.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);
            }
        }
    }