public ActionResult Index(int id, int? itemId, int? formId, string column, bool? ascending)
 {
     RepositoryViews repositoryViews = new RepositoryViews();
     Models.View view = repositoryViews.Get(id);
     if (formId.HasValue)
         view.Group.DefaultFormId = formId.Value;
     if (itemId.HasValue)
     {
         //2 = Edit
         //3 = Delete
         //4 = Details
         if (view.ViewTypeId == 2 || view.ViewTypeId == 3 || view.ViewTypeId == 4)
         {
             RepositoryMapguide mapguide = new RepositoryMapguide();
             DataViewModel model = new DataViewModel()
             {
                 ItemId = itemId.Value,
                 View = view,
                 Form = view.Group.Forms.Single(m => m.FormId == view.Group.DefaultFormId)
             };
             model = mapguide.Get(model);
             return View(view.Name, model);
         }
     }
     else
     {
         //1 = Create
         if (view.ViewTypeId == 1)
         {
             DataViewModel model = new DataViewModel
             {
                 View = view,
                 Form = view.Group.Forms.SingleOrDefault(m => m.FormId == view.Group.DefaultFormId),
                 Date = DateTime.Now,
                 UniqueId = Guid.NewGuid().ToString()
             };
             if (User.Identity.IsAuthenticated)
             {
                 model.UserName = User.Identity.Name;
                 model.UserId = Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString();
                 UserProfile profile = UserProfile.GetUserProfile(User.Identity.Name);
                 RepositoryCompanies rc = new RepositoryCompanies();
                 Company company = rc.Get(profile.CompanyId);
                 if (company != null)
                     model.Company = company.Name;
             }
             return View(view.Name, model);
         }
         //5 = List All
         else if (view.ViewTypeId == 5)
         {
             RepositoryMapguide mapguide = new RepositoryMapguide();
             DataListModel model = mapguide.GetList(view, "");
             return View(view.Name, model);
         }
         //6 = List My
         else if (view.ViewTypeId == 6)
         {
             RepositoryMapguide mapguide = new RepositoryMapguide();
             DataListModel model = null;
             if (User.Identity.IsAuthenticated)
             {
                 model = mapguide.GetList(view, Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString());
             }
             else
             {
                 model = mapguide.GetList(view, "");
             }
             return View(view.Name, model);
         }
     }
     return View("Error");
 }
        public ActionResult Delete(DataViewModel postModel)
        {
            RepositoryMapguide mapguide = new RepositoryMapguide();
            RepositoryViews repository = new RepositoryViews();
            Models.View view = repository.Get(postModel.View.ViewId);
            DataViewModel model = new DataViewModel
            {
                View = view,
                Form = view.Group.Forms.Single(m => m.FormId == postModel.Form.FormId),
                ItemId = postModel.ItemId,
                UniqueId = postModel.UniqueId
            };
            model = mapguide.Get(model);
            mapguide.Delete(model);
            List<DataEmailModel> emails = new List<DataEmailModel>();
            if (!String.IsNullOrEmpty(model.Form.Email)&&!String.IsNullOrEmpty(model.Form.ViewEmailToReceiverOnDelete))
            {
                emails.Add(new DataEmailModel { Email = model.Form.Email, View = model.Form.ViewEmailToReceiverOnDelete });
            }
            foreach (Field field in model.Form.Fields)
            {
                if (field.FieldTypeId == 7)
                {
                    if (!string.IsNullOrEmpty(field.Data))
                    {
                        if (email.IsMatch(field.Data))
                        {
                            string emailView = field.Form.ViewEmailToSenderOnDelete;
                            Field operationField = field.Form.Fields.FirstOrDefault(m => m.FieldTypeId == 13);
                            if (!String.IsNullOrEmpty(emailView) && operationField != null)
                            {
                                if (!String.IsNullOrEmpty(operationField.Data))
                                {
                                    if (operationField.Data == "1" || operationField.Data == "True")
                                    {
                                        emails.Add(new DataEmailModel { Email = field.Data, View = emailView });
                                    }
                                }

                            }
                        }
                    }
                }
                Selection selection = field.Selections.SingleOrDefault(m => m.Name == field.Data);
                if (selection != null && !String.IsNullOrEmpty(model.Form.ViewEmailToReceiverOnDelete))
                {

                    if (!String.IsNullOrEmpty(selection.Email))
                    {
                            emails.Add(new DataEmailModel { Email = selection.Email, View = model.Form.ViewEmailToReceiverOnDelete });
                    }
                }
            }
            DataCreateModel dcm = new DataCreateModel()
            {
                Model = model,
                Emails = emails
            };
            mapguide.SendEmails(dcm);
            Models.View endView = model.View.Group.Views.SingleOrDefault(m => m.ViewTypeId == 7);
            if (endView == null)
            {
                Models.View listUser = model.View.Group.Views.SingleOrDefault(m => m.ViewTypeId == 6);
                if (listUser == null)
                {
                    Models.View listAll = model.View.Group.Views.SingleOrDefault(m => m.ViewTypeId == 5);
                    if (listAll == null)
                    {
                        return View(model.View.Name, model);
                    }
                    else
                    {
                        return RedirectToAction("Index", new { id = listAll.ViewId, formId = model.Form.FormId });
                    }
                }
                else
                {
                    return RedirectToAction("Index", new { id = listUser.ViewId, formId = model.Form.FormId });
                }

            }
            else
            {
                return View(endView.Name, model);
            }
        }