Пример #1
0
        public SuggestionBase AddNewSuggestion(SuggestionAdd newItem)
        {
            //add new item variable
            var addedItem = new Suggestion();

            //Convert suggestionAdd to suggestion
            addedItem = Mapper.Map <Suggestion>(newItem);

            //find program information & course information
            addedItem.Program = ds.Programs.Find(newItem.ProgramId).Name;
            addedItem.Course  = ds.Courses.Find(newItem.CourseId).Name;

            //Handle the uploaded image
            //Add attachment only if there is image
            if (newItem.AttachmentUpload != null)
            {
                byte[] attachmentBytes = new byte[newItem.AttachmentUpload.ContentLength];
                newItem.AttachmentUpload.InputStream.Read(attachmentBytes, 0, newItem.AttachmentUpload.ContentLength);

                //ojbect property
                addedItem.Attachment  = attachmentBytes;
                addedItem.ContentType = newItem.AttachmentUpload.ContentType;
            }
            //insert into data & save
            ds.Suggestions.Add(addedItem);
            ds.SaveChanges();

            return((addedItem == null) ? null : Mapper.Map <SuggestionBase>(addedItem));
        }
        public ActionResult Create(SuggestionAdd newItem)
        {
            if (!ModelState.IsValid)
            {
                return(HttpNotFound());
            }
            else
            {
                var addedItem = m.AddNewSuggestion(newItem);

                if (addedItem == null)
                {
                    return(HttpNotFound());
                }
                else
                {
                    return(RedirectToAction("index"));
                }
            }
        }