private void ApplyDefaultDataToContentModel(PageMetadataProvider pageMetadataProvider, object contentModel, string header = null) { if (string.IsNullOrEmpty(header) && !string.IsNullOrEmpty(options.DefaultPageHeader)) { pageMetadataProvider.SetPageHeader(contentModel, options.DefaultPageHeader); } var view = viewLocator.FindView(pageMetadataProvider.ContentType); if (view == null) { throw new InvalidOperationException(); } if (view.DefaultModelData != null) { pageMetadataProvider.ContentMetadata.ApplyDataToModel(view.DefaultModelData, contentModel); } if (string.IsNullOrEmpty(header)) { header = pageMetadataProvider.Title; } pageMetadataProvider.SetPageHeader(contentModel, header); }
public async Task <IActionResult> ChangeModelTypeAsync([FromQuery] Guid editId, [FromQuery] string modelPath, [FromQuery] string modelType, [FromServices] IContentMetadataManager contentMetadataManager, [FromServices] Views.IViewLocator viewLocator) { if (modelType == null) { return(BadRequest()); } var editSession = await pageContentService.FindEditByIdAsync(editId); if (editSession == null) { return(BadRequest()); } var page = await pageService.FindPageByIdAsync(editSession.PageId); if (page == null) { return(BadRequest()); } if (modelPath == null) { modelPath = string.Empty; } var newModelType = contentMetadataManager.GetMetadata(modelType); var pageContent = await pageContentService.GetContentAsync(editSession); var pageContentExplorer = ContentExplorer.Create(contentMetadataManager, pageContent); var contentExplorer = pageContentExplorer.Navigate(modelPath); if (contentExplorer == null) { return(BadRequest()); } contentExplorer.Field.ChangeType(contentExplorer.Model, modelType); var newItem = newModelType.CreateModelInstance(); var view = viewLocator.FindView(newModelType.ModelType); if (view != null && view.DefaultModelData != null) { newItem = newModelType.ConvertDictionaryToContentModel(view.DefaultModelData); } return(Ok()); }