public ActionResult Create(DataViewModel postModel, FormCollection formValues)
        {
            DataCreateModel dataCreateModel = CreateModel(postModel, formValues,Operation.Create);
            DataViewModel model = dataCreateModel.Model;
            foreach(Field field in model.Form.Fields.Where(m => m.FieldTypeId == 11))
            {
                field.Data = Request.UserHostAddress;
            }

            if (ModelState.IsValid)
            {
                RepositoryMapguide repositoryMapGuide = new RepositoryMapguide();
                model = repositoryMapGuide.Add(model);
                dataCreateModel.Model = model;
                repositoryMapGuide.SendEmails(dataCreateModel);
                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
                {
                    ViewBag.Id = model.View.ViewId;
                    model.View = endView;
                    return View(endView.Name, model);
                }
            }
            else
                return View(model.View.Name, model);
        }
        //public async Task<HttpResponseMessage> PostFormData()
        public Task<HttpResponseMessage> PostFormData()
        {
            string root = HttpContext.Current.Server.MapPath("~/App_Data");
            /*StreamWriter log1;
            string file1 = Path.Combine(@"d:\inetpub\wwwroot\rapportfrastedet\app_data\", "log1.txt");
            if (!File.Exists(file1))
            {
                log1 = new StreamWriter(file1);
            }
            else
            {
                log1 = File.AppendText(file1);
            }

            // Write to the file:
            log1.WriteLine(DateTime.Now);
            log1.WriteLine(root);

            // Close the stream:
            log1.Close();
            */
            // Check if the request contains multipart/form-data.
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            var provider = new MultipartFormDataStreamProvider(root);

            /**/
            // Read the form data.
            //await Request.Content.ReadAsMultipartAsync(provider);
            return Request.Content.ReadAsMultipartAsync(provider).ContinueWith(data =>
            {//.Wait();

                // This illustrates how to get the file names.
                /*foreach (MultipartFileData file in provider.FileData)
                {
                    Trace.WriteLine(file.Headers.ContentDisposition.FileName);
                    Trace.WriteLine("Server file path: " + file.LocalFileName);
                }*/
                try
                {
                    HttpContextWrapper http = (HttpContextWrapper)Request.Properties["MS_HttpContext"];
                    HttpContext.Current = http.ApplicationInstance.Context;
                    RepositorySaveData r = new RepositorySaveData();
                    DataCreateModel dataCreateModel = r.CreateModel(data.Result.FormData, data.Result.FileData, Operation.Create);
                    DataViewModel model = dataCreateModel.Model;

                    foreach (Field field in model.Form.Fields)
                    {
                        if (field.FieldTypeId == 11)
                            field.Data = http.Request.UserHostAddress;
                    }
                    RepositoryMapguide repositoryMapGuide = new RepositoryMapguide();
                    model = repositoryMapGuide.Add(model);
                    dataCreateModel.Model = model;
                    repositoryMapGuide.SendEmails(dataCreateModel);
                    return Request.CreateResponse(HttpStatusCode.OK);
                }
                catch (System.Exception e)
                {
                    StreamWriter log;
                    string file = Path.Combine(root, "log.txt");
                    if (!File.Exists(file))
                    {
                        log = new StreamWriter(file);
                    }
                    else
                    {
                        log = File.AppendText(file);
                    }

                    // Write to the file:
                    log.WriteLine(DateTime.Now);
                    log.WriteLine(e.Message);
                    log.WriteLine(e.StackTrace);

                    // Close the stream:
                    log.Close();
                    HttpResponseMessage r = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
                    System.Net.Http.Headers.MediaTypeWithQualityHeaderValue json = new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json");
                    System.Net.Http.Headers.MediaTypeWithQualityHeaderValue plain = new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/plain");
                    if (!Request.Headers.Accept.Contains(json))
                    {
                        r.Content.Headers.ContentType = plain;
                    }
                    return r;

                }
            });
        public ActionResult Items(int id, int formId)
        {
            RepositoryViews repository = new RepositoryViews();
            Models.View view = repository.Get(id);
            view.Group.DefaultFormId = formId;
            RepositoryMapguide mapguide = new RepositoryMapguide();
            DataListModel model = null;

            if (view.ViewTypeId == 5 || !User.Identity.IsAuthenticated)
            {
                model = mapguide.GetList(view, "");
            }
            else
            {
                model = mapguide.GetList(view, Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString());
            }
            return PartialView("System_Items", model);
        }
 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 Edit(DataViewModel postModel, FormCollection formValues)
        {
            DataCreateModel dataCreateModel = CreateModel(postModel, formValues,Operation.Edit);
            DataViewModel model = dataCreateModel.Model;
            if (ModelState.IsValid)
            {
                RepositoryMapguide repositoryMapGuide = new RepositoryMapguide();
                repositoryMapGuide.Update(model);
                repositoryMapGuide.SendEmails(dataCreateModel);
                Models.View list = null;
                try
                {
                    list = model.View.Group.Views.Single(m => m.ViewTypeId == 5);
                }
                catch { }
                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
                {
                    ViewBag.Id = model.View.ViewId;
                    model.View = endView;
                    return View(endView.Name, model);
                }
            }
            else
                return View(model.View.Name, model);
        }
        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);
            }
        }