public ActionResult _EventCreateInstance(int evtId, string evtDate)
        {
            var query = from evt in context.CEvents
                        where evt.id == evtId
                        select evt;
            //clone the event
            CEvent myEvent       = query.First();
            CEvent myClonedEvent = myEvent.DeepCopy();

            //set the recurring to false
            myClonedEvent.Recurring      = false;
            myClonedEvent.recurranceType = (int)Recurrance.None;
            //the existence of this ref will also hide recurrance options from the form
            myClonedEvent.recurranceRef = myEvent.id;

            myClonedEvent.EventDate = DateTime.Parse(evtDate);
            //reset the start and end datetime values

            string myNewStartDate = myClonedEvent.EventDate.ToShortDateString() + " " + DateTime.Parse(myEvent.start.ToString()).ToShortTimeString();

            myClonedEvent.start = DateTime.Parse(myNewStartDate);

            string myNewEndDate = myClonedEvent.EventDate.ToShortDateString() + " " + DateTime.Parse(myEvent.end.ToString()).ToShortTimeString();

            myClonedEvent.end = DateTime.Parse(myNewEndDate);

            myClonedEvent.id = context.CEvents.OrderByDescending(evt => evt.id).FirstOrDefault().id + 1;

            int myNewEventId;

            try
            {
                context.Add <CEvent>(myClonedEvent);
                myNewEventId = context.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                        ModelState.AddModelError(ve.PropertyName, ve.ErrorMessage);
                    }
                }
            }

            return(PartialView("_EventDialogue", myClonedEvent));
        }
示例#2
0
        public WrappedJsonResult _ImageUploadForm(FormCollection myForm, HttpPostedFileWrapper imageFile)
        {
            if (imageFile == null || imageFile.ContentLength == 0)
            {
                return(new WrappedJsonResult
                {
                    Data = new
                    {
                        IsValid = false,
                        Message = "No file was uploaded.",
                        ImagePath = string.Empty
                    }
                });
            }

            //------------------------------------My Stuff---------------------------------//
            CImage myUploadImage = new CImage();

            myUploadImage.title         = myForm["title"];
            myUploadImage.description   = myForm["description"];
            myUploadImage.createdDate   = DateTime.Today;
            myUploadImage.type          = 1;//(int)CImage.ImageType.Event;
            myUploadImage.userName      = "******";
            myUploadImage.createdDate   = DateTime.Now;
            myUploadImage.imageMimeType = imageFile.ContentType;
            myUploadImage.imageFile     = new byte[imageFile.ContentLength];
            imageFile.InputStream.Read(myUploadImage.imageFile, 0, imageFile.ContentLength);

            try
            {
                context.Add <CImage>(myUploadImage);
                context.SaveChanges();
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    //Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                    //eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        //Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                        //ve.PropertyName, ve.ErrorMessage);
                        ModelState.AddModelError(ve.PropertyName, ve.ErrorMessage);
                    }
                }
            }

            //-------------------------------------------My Stuff ends-------------------------------------------//
            if (!ModelState.IsValid)
            {
                var myResult = new WrappedJsonResult
                {
                    Data = new
                    {
                        IsValid   = false,
                        Message   = RenderPartialViewToString("_ImageUploadForm", myUploadImage),
                        ImagePath = ""
                    }
                };
                return(myResult);
            }

            var fileName = String.Format("{0}.jpg", Guid.NewGuid().ToString());

            /*var imagePath = Path.Combine(Server.MapPath(Url.Content("~/Uploads")), fileName);
             *
             * imageFile.SaveAs(imagePath);*/

            return(new WrappedJsonResult
            {
                Data = new
                {
                    IsValid = true,
                    Message = "",
                    ImagePath = Url.Content(String.Format("~/Uploads/{0}", fileName))
                }
            });
        }