PhotoEntity.Controllers.Manager.PropertyPhotoAdd C# (CSharp) Method

PropertyPhotoAdd() public method

public PropertyPhotoAdd ( PhotoAdd newItem ) : PropertyBase
newItem PhotoAdd
return PropertyBase
        public PropertyBase PropertyPhotoAdd(PhotoAdd newItem)
        {
            // Validate the associated item
            var a = ds.Properties.Find(newItem.PropertyId);

            if (a == null)
            {
                return null;
            }
            else
            {
                // Attempt to add the new item
                var addedItem = new Photo();
                ds.Photos.Add(addedItem);

                addedItem.Caption = newItem.Caption;
                addedItem.Property = a;

                // Handle the uploaded photo...

                // First, extract the bytes from the HttpPostedFile object
                byte[] photoBytes = new byte[newItem.PhotoUpload.ContentLength];
                newItem.PhotoUpload.InputStream.Read(photoBytes, 0, newItem.PhotoUpload.ContentLength);

                // Then, configure the new object's properties
                addedItem.Content = photoBytes;
                addedItem.ContentType = newItem.PhotoUpload.ContentType;

                ds.SaveChanges();

                return (addedItem == null) ? null : Mapper.Map<PropertyBase>(a);
            }
        }

Usage Example

コード例 #1
0
        public ActionResult AddPhoto(int?id, PhotoAdd newItem)
        {
            // Validate the input
            // Two conditions must be checked
            if (!ModelState.IsValid && id.GetValueOrDefault() == newItem.PropertyId)
            {
                return(View(newItem));
            }

            // Process the input
            var addedItem = m.PropertyPhotoAdd(newItem);

            if (addedItem == null)
            {
                return(View(newItem));
            }
            else
            {
                return(RedirectToAction("Details", new { id = addedItem.Id }));
            }
        }