コード例 #1
0
        /// <summary>
        /// Intercepts the view data to get a reference to the code-first model, then
        /// passes the render model down to the base method (thus allowing the @Model property
        /// to behave as it usually does in an Umbraco view)
        /// </summary>
        /// <param name="viewData">The view data for the request</param>
        protected override void SetViewData(ViewDataDictionary viewData)
        {
            if (viewData.Model is RenderModel)
            {
                var doc = (viewData.Model as RenderModel).Content.ConvertToModel();
                if (doc.GetType() != typeof(Tdocument) && !doc.GetType().Inherits <Tdocument>())
                {
                    throw new CodeFirstException("Wrong type of model. This model does not inherit " + typeof(Tdocument).FullName);
                }
                base.SetViewData(viewData);
                Tviewmodel vm;
                ModelEventDispatcher <Tdocument> .OnLoad((Tdocument)doc, out vm, Umbraco.AssignedContentItem, Context, UmbracoContext, ApplicationContext, Core.CodeFirstModelContext.GetContext(doc));

                _innerModel = new DocumentViewModel <Tdocument, Tviewmodel>((viewData.Model as RenderModel), (Tdocument)doc, vm);
            }
            else if (viewData.Model is DocumentViewModel <Tdocument, Tviewmodel> )
            {
                _innerModel    = viewData.Model as DocumentViewModel <Tdocument, Tviewmodel>;
                viewData.Model = _innerModel.RenderModel;
                base.SetViewData(viewData);
                ModelEventDispatcher <Tdocument> .OnLoad(UnderlyingViewModel.Document, Umbraco.AssignedContentItem, Context, UmbracoContext, ApplicationContext, Core.CodeFirstModelContext.GetContext(UnderlyingViewModel.Document));
            }
            else
            {
                throw new CodeFirstException("Wrong type of model. This view requires either a RenderModel (if the document type or its specified event handler implements IOnRender<Tdocument,Tviewmodel>) or DocumentViewModel<Tdocument, Tviewmodel> (if you're using a custom RenderMvcController). Note that CodeFirstManager.Current.Features.EnableContentEvents must be true to use IOnRender.");
            }
        }
コード例 #2
0
 protected UmbracoDocumentViewPage() : base()
 {
     if (CodeFirstManager.Current.Features.EnableContentEvents && ModelEventDispatcher.HasEvent <IOnLoadBase>(typeof(Tdocument)))
     {
         var doc = GetDocument();
         _helper = new Lazy <CodeFirstDocumentHelper <Tdocument> >(() => new CodeFirstDocumentHelper <Tdocument>(Html, doc));
     }
     else
     {
         _helper = new Lazy <CodeFirstDocumentHelper <Tdocument> >(() => new CodeFirstDocumentHelper <Tdocument>(Html, GetDocument()));
     }
 }
コード例 #3
0
        /// <summary>
        /// Extension used to convert an IPublishedContent back to a Typed model instance.
        /// Your model does need to inherit from UmbracoGeneratedBase and contain the correct attributes
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="content"></param>
        /// <returns></returns>
        protected Tmodel ConvertToModelInternal <Tmodel>(IPublishedContent content, CodeFirstModelContext parentContext = null) where Tmodel : CodeFirstContentBase <T>
        {
            ContentTypeRegistration registration;

            if (content == null)
            {
                throw new ArgumentNullException("content", Environment.StackTrace);
            }

            if (!_contentTypeModule.TryGetContentType(content.ContentType.Alias, out registration))
            {
                throw new CodeFirstException("Could not find content type registration for content type alias " + content.ContentType.Alias);
            }
            if (registration.ClrType != typeof(Tmodel))
            {
                if (registration.ClrType.Inherits(typeof(Tmodel)))
                {
                    if (typeof(Tmodel).GetCodeFirstAttribute <ContentTypeAttribute>(false) == null)                    //The base type is not an Umb content type, just create the full document and cast the result
                    {
                    }
                    else if (!_contentTypeModule.TryGetContentType(typeof(Tmodel), out registration)) //Redirect to the underlying type and make one of those instead
                    {
                        throw new CodeFirstException("Could not find content type registration for underlying type " + typeof(Tmodel).FullName);
                    }
                }
                else
                {
                    throw new CodeFirstException("Registered type for content type " + content.ContentType.Alias + " is " + registration.ClrType.Name + ", not " + typeof(Tmodel).Name);
                }
            }

            Tmodel instance = (Tmodel)CreateInstanceFromPublishedContent(content, registration, parentContext);

            if (instance == null)
            {
                throw new CodeFirstException("Model could not be created. Target type: " + typeof(Tmodel).Name);
            }

            if ((instance as CodeFirstContentBase <T>) == null)
            {
                throw new CodeFirstException("Created model could not be cast to CodeFirstContentBase<T>. Type of T: " + typeof(T).Name);
            }

            (instance as CodeFirstContentBase <T>).NodeDetails = new T();
            ((instance as CodeFirstContentBase <T>).NodeDetails as ContentNodeDetails).Initialise(content);
            ModelEventDispatcher <Tmodel> .OnLoad(instance, content, new HttpContextWrapper(HttpContext.Current), UmbracoContext.Current, ApplicationContext.Current, CodeFirstModelContext.GetContext(instance));

            return(instance);
        }