public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
 {
     DynamicContents dc = new DynamicContents();
     var form = controllerContext.HttpContext.Request.Form;
     foreach (var key in form.AllKeys)
     {
         dc.TrySetMember(key, form[key]);
     }
     return dc;
 }
Beispiel #2
0
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="dc"></param>
        public void SaveContents(DynamicContents dc)
        {
            using (var dmTrans = _rep.GetTransaction())
            {
                //创建上下文
                CommonModelPartContext ctx = new CommonModelPartContext();
                try
                {
                    ctx.Content = dc;
                    //开始事务
                    var trans = dmTrans.BeginTransaction();
                    ctx.Trans = trans;

                    //获取栏目
                    var node = _rep.Get<ENode>(p => p.Id == ctx.Content.NodeId);
                    if (node == null) { throw new NullReferenceException("没有找到当前栏目!NodeId=" + ctx.Content.NodeId); }
                    ctx.Node = node;

                    //获取模型
                    var model = _rep.Get<ECommonModel>(p => p.Id == node.NodeCommonModelId);
                    if (model == null) { throw new NullReferenceException("没有找到当前模型!CommonModelId=" + node.NodeCommonModelId); }
                    ctx.Model = model;

                    //获取所有部件
                    var modelParts = _rep.GetList<ECommonModelPart>(0, p => p.CommonModelId == ctx.Model.Id).ToList();
                    modelParts.ForEach(mp =>
                    {
                        var part = CdevContainer.Ins.Resolve<AbstractCommonModelPart>(mp.PartName);
                        part.Rebuild(mp);
                        ctx.Parts.Add(part);
                    });

                    //触发添加前事件
                    foreach (var part in ctx.Parts)
                    {
                        part.OnBeforeEditContent(ctx);
                    }

                    //映射核心内容
                    EContents content = DMapper.Map<EContents, DynamicContents>(ctx.Content);
                    //更新核心内容
                    _rep.Save(content, p => p.Id == ctx.Content.Id, trans);
                    dc.SetMember("ItemId", content.Id);

                    //添加附加内容
                    var collection = ctx.Content.GetFormCollection();
                    StringBuilder sb = new StringBuilder();
                    sb.AppendFormat("UPDATE cms_contents_{0} ", ctx.Model.CommonModelTableName);
                    sb.AppendFormat("SET {0} WHERE ItemId=?ItemId", string.Join(",", collection.Keys.Select(p => p + " = ?" + p)));
                    var cmd = DMContext.TSqlCommand(sb.ToString()).SetTrans(trans);
                    foreach (var item in collection)
                    {
                        cmd.AddParameter(item.Key, item.Value);
                    }
                    var count = cmd.ExecuteNonQuery();
                    if (count == 0) throw new Exception("更新失败!未知错误!ContentId=" + ctx.Content.Id);

                    //触发添加后事件
                    foreach (var part in ctx.Parts)
                    {
                        part.OnBeforeEditContent(ctx);
                    }

                    //提交
                    dmTrans.Commit();

                    //触发提交后事件
                    foreach (var part in ctx.Parts)
                    {
                        part.OnAfterCommitEditContent(ctx);
                    }
                }
                catch
                {
                    //异常回滚
                    dmTrans.Rollback();

                    //触发提交后事件
                    foreach (var part in ctx.Parts)
                    {
                        part.OnAfterRollbackEditContent(ctx);
                    }
                    throw;
                }
            }
        }