public ActionResult Add(int sourceType, int sourceId, AttachmentFormModel model)
        {
            var attachment = AttachmentService.AttachmentNew((SourceType)sourceType, sourceId);

            Csla.Data.DataMapper.Map(model, attachment, true);

            var fs = model.FileData.InputStream;
            var fileData = new byte[model.FileData.ContentLength];
            fs.Read(fileData, 0, model.FileData.ContentLength);

            attachment.FileData = fileData;
            attachment.Name = Path.GetFileName(model.FileData.FileName);
            attachment.FileType = model.FileData.ContentType;

            attachment = AttachmentService.AttachmentSave(attachment);

            if (attachment.IsValid)
            {
                attachment = AttachmentService.AttachmentFetch(attachment.AttachmentId);

                this.Map(attachment, model, true);
            }

            return PartialView("AttachmentUserControl", model);
        }
        public ActionResult Create(int noteId)
        {
            var model = new AttachmentFormModel();
            var attachment = AttachmentRepository.AttachmentNew(noteId, SourceType.Note);

            model.Title = "Attachment Create";
            model.Attachment = attachment;

            return this.View(model);
        }
        public ActionResult Create(int noteId, FormCollection collection)
        {
            var model = new AttachmentFormModel();
            var note = NoteRepository.NoteFetch(noteId);
            var attachment = AttachmentRepository.AttachmentNew(noteId, SourceType.Note);

            this.Map(collection, attachment);

            attachment = AttachmentRepository.AttachmentSave(attachment);

            if (attachment.IsValid)
            {
                return this.RedirectToAction("Details", note.SourceTypeName, new { id = note.SourceId });
            }

            model.Title = "Attachment Create";
            model.Attachment = attachment;

            ModelHelper.MapBrokenRules(this.ModelState, attachment);

            return this.View(model);
        }
        public AttachmentFormModel Map(Attachment attachment, AttachmentFormModel model, bool ignoreBrokenRules)
        {
            Csla.Data.DataMapper.Map(attachment, model, true);

            model.Tab = "Task";
            model.IsNew = attachment.IsNew;
            model.IsValid = attachment.IsValid;

            if (!ignoreBrokenRules)
            {
                foreach (var brokenRule in attachment.BrokenRulesCollection)
                {
                    this.ModelState.AddModelError(string.Empty, brokenRule.Description);
                }
            }

            return model;
        }
        public ActionResult Edit(int id)
        {
            var model = new AttachmentFormModel();
            var attachment = AttachmentRepository.AttachmentFetch(id);

            model.Title = "Attachment Edit";
            model.Attachment = attachment;

            return this.View(model);
        }