public static List<Common.Models.Forms.FormFieldMatter> ListForMatter(Guid matterId)
        {
            List<Common.Models.Forms.FormFieldMatter> list;
            List<Common.Models.Forms.FormField> fieldList;

            fieldList = Data.Forms.FormField.List();
            list = new List<Common.Models.Forms.FormFieldMatter>();

            fieldList.ForEach(x =>
            {
                Common.Models.Forms.FormFieldMatter ffm;

                ffm = Data.Forms.FormFieldMatter.Get(matterId, x.Id.Value);

                if (ffm == null)
                    ffm = new Common.Models.Forms.FormFieldMatter() 
                    { 
                        FormField = x
                    };
                else
                    ffm.FormField = x;

                list.Add(ffm);
            });

            return list;
        }
        public static List<Common.Models.Forms.FormFieldMatter> ListForMatter(
            Guid matterId,
            IDbConnection conn = null, 
            bool closeConnection = true)
        {
            List<Common.Models.Forms.FormFieldMatter> list;
            List<Common.Models.Forms.FormField> fieldList;

            fieldList = Data.Forms.FormField.List(conn, false);
            list = new List<Common.Models.Forms.FormFieldMatter>();

            fieldList.ForEach(x =>
            {
                Common.Models.Forms.FormFieldMatter ffm;

                ffm = Data.Forms.FormFieldMatter.Get(matterId, x.Id.Value, conn, false);

                if (ffm == null)
                    ffm = new Common.Models.Forms.FormFieldMatter() 
                    { 
                        FormField = x
                    };
                else
                    ffm.FormField = x;

                list.Add(ffm);
            });

            DataHelper.Close(conn, closeConnection);

            return list;
        }
        public ActionResult FormFields(Guid id, IEnumerable<ViewModels.Forms.FormFieldMatterViewModel> viewModel)
        {
            Common.Models.Matters.Matter matter;
            Common.Models.Account.Users currentUser;

            using (Data.Transaction trans = Data.Transaction.Create(true))
            {
                try
                {
                    currentUser = Data.Account.Users.Get(trans, User.Identity.Name);
                    matter = Data.Matters.Matter.Get(trans, id);

                    viewModel.Each(x =>
                    {
                        Common.Models.Forms.FormFieldMatter model =
                            Data.Forms.FormFieldMatter.Get(trans, matter.Id.Value, x.FormField.Id.Value);

                        if (model != null)
                        {
                            //model = Data.Forms.FormFieldMatter.Get(x.Id.Value);
                            // Edit
                            if (model.Value != x.Value)
                            {
                                model.Value = x.Value;
                                model = Data.Forms.FormFieldMatter.Edit(trans, model, currentUser);
                            }
                        }
                        else
                        {
                            // Create
                            model = new Common.Models.Forms.FormFieldMatter();
                            model.FormField = new Common.Models.Forms.FormField();
                            model.FormField.Id = x.FormField.Id;
                            model.Matter = matter;
                            model.Value = x.Value;
                            model = Data.Forms.FormFieldMatter.Create(trans, model, currentUser);
                        }

                    });

                    trans.Commit();

                    return RedirectToAction("Details", new { Id = id });
                }
                catch
                {
                    trans.Rollback();
                    return FormFields(id);
                }
            }
        }
        public ActionResult FormFields(Guid id, IEnumerable<ViewModels.Forms.FormFieldMatterViewModel> viewModel)
        {
            Common.Models.Matters.Matter matter;
            Common.Models.Account.Users currentUser;

            currentUser = Data.Account.Users.Get(User.Identity.Name);
            matter = Data.Matters.Matter.Get(id);

            viewModel.Each(x =>
            {
                Common.Models.Forms.FormFieldMatter model = Data.Forms.FormFieldMatter.Get(matter.Id.Value, x.FormField.Id.Value);

                if (model != null)
                {
                    //model = Data.Forms.FormFieldMatter.Get(x.Id.Value);
                    // Edit
                    if (model.Value != x.Value)
                    {
                        model.Value = x.Value;
                        model = Data.Forms.FormFieldMatter.Edit(model, currentUser);
                    }
                }
                else
                {
                    // Create
                    model = new Common.Models.Forms.FormFieldMatter();
                    model.FormField = new Common.Models.Forms.FormField();
                    model.FormField.Id = x.FormField.Id;
                    model.Matter = matter;
                    model.Value = x.Value;
                    model = Data.Forms.FormFieldMatter.Create(model, currentUser);
                }

            });

            return RedirectToAction("Details", new { Id = id });
        }