예제 #1
0
        public async Task <IActionResult> Create()
        {
            var context     = CreateDomainContext();
            var forumDomain = DomainProvider.GetService <EntityDomainService <Entity.Forum> >();
            IEntityEditModel <Entity.Forum> forumResult;

            try
            {
                forumResult = await forumDomain.ExecuteAsync <IEntityEditModel <Entity.Forum> >(context, "Detail");

                ViewBag.Forum = forumResult.Item;
            }
            catch (ArgumentException ex)
            {
                return(NotFound());
            }
            catch (EntityNotFoundException ex)
            {
                return(NotFound());
            }
            var threadDomain = DomainProvider.GetService <EntityDomainService <Thread> >();
            IEntityEditModel <Thread> threadResult = await threadDomain.ExecuteAsync <IEntityEditModel <Thread> >(context, "Create");

            threadResult.Item.Forum = forumResult.Item;
            return(View(threadResult));
        }
예제 #2
0
        /// <summary>
        /// 输出一个编辑器。
        /// </summary>
        /// <typeparam name="TEntity">实体类型。</typeparam>
        /// <param name="helper">Html帮助器。</param>
        /// <param name="model">实体编辑模型。</param>
        /// <param name="expression">属性选择表达式。</param>
        /// <returns></returns>
        public static IHtmlContent Editor <TEntity>(this IHtmlHelper helper, IEntityEditModel <TEntity> model, Expression <Func <TEntity, object> > expression)
            where TEntity : class, IEntity, new()
        {
            if (helper == null)
            {
                throw new ArgumentNullException(nameof(helper));
            }
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }
            if (!(expression.Body is MemberExpression))
            {
                throw new NotSupportedException();
            }
            MemberExpression memberExpression = (MemberExpression)expression.Body;

            if (!(memberExpression.Expression is ParameterExpression))
            {
                throw new NotSupportedException();
            }
            var value    = expression.Compile()(model.Item);
            var property = model.Metadata.GetProperty(memberExpression.Member.Name);

            return(Editor(helper, model.Item, property, value));
        }
예제 #3
0
        public async Task <IActionResult> Create()
        {
            var context     = CreateDomainContext();
            var forumDomain = DomainProvider.GetService <EntityDomainService <Entity.Forum> >();
            IEntityEditModel <Entity.Forum> forumResult;

            try
            {
                forumResult = await forumDomain.ExecuteAsync <IEntityEditModel <Entity.Forum> >(context, "Detail");

                ViewBag.Forum = forumResult.Item;
            }
            catch (DomainServiceException ex)
            {
                if (ex.InnerException is ArgumentException)
                {
                    return(StatusCode(400, ex.InnerException.Message));
                }
                else if (ex.InnerException is EntityNotFoundException)
                {
                    return(NotFound());
                }
                else
                {
                    ExceptionDispatchInfo.Capture(ex).Throw();
                    throw;
                }
            }
            var threadDomain = DomainProvider.GetService <EntityDomainService <Thread> >();
            IEntityEditModel <Thread> threadResult = await threadDomain.ExecuteAsync <IEntityEditModel <Thread> >(context, "Create");

            threadResult.Item.Forum = forumResult.Item;
            return(View(threadResult));
        }
예제 #4
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            IEntityEditModel model = (IEntityEditModel)value;

            writer.WriteStartObject();

            writer.WritePropertyName("Item");
            serializer.Serialize(writer, model.Item);

            writer.WritePropertyName("Properties");
            serializer.Serialize(writer, model.Properties);

            writer.WritePropertyName("Metadata");
            serializer.Serialize(writer, model.Metadata);

            writer.WriteEndObject();
        }
예제 #5
0
        /// <summary>
        /// Render a property viewer.
        /// </summary>
        /// <typeparam name="TEntity">Type of entity.</typeparam>
        /// <param name="helper">A html helper.</param>
        /// <param name="model">Entity model.</param>
        /// <param name="expression">Expression for property to entity.</param>
        /// <returns></returns>
        public static MvcHtmlString Viewer <TEntity>(this HtmlHelper helper, IEntityEditModel <TEntity> model, Expression <Func <TEntity, object> > expression)
            where TEntity : class, IEntity, new()
        {
            if (!(expression.Body is MemberExpression))
            {
                throw new NotSupportedException();
            }
            MemberExpression memberExpression = (MemberExpression)expression.Body;

            if (!(memberExpression.Expression is ParameterExpression))
            {
                throw new NotSupportedException();
            }
            var value    = expression.Compile()(model.Item);
            var property = model.Metadata.GetProperty(memberExpression.Member.Name);

            return(Viewer(helper, model.Item, property, value));
        }