Advertisement
garfbradaz

My MVC3 Create ActionMethod

Apr 19th, 2012
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.50 KB | None | 0 0
  1.     //
  2.         // POST: /LedgerUser/Create[Bind(Prefix = "LedgerUser")]
  3.  
  4.         [HttpPost]
  5.         public ActionResult Create(bool Thumbnail,LedgerUser LedgerUser, HttpPostedFileBase imageLoad2)
  6.         {
  7.             var Avatar = new Accounts.Models.Image();
  8.            
  9.             //--Handle the image first.
  10.             if (imageLoad2 != null)
  11.             {
  12.                 using (System.Drawing.Image img = System.Drawing.Image.FromStream(imageLoad2.InputStream))
  13.                 {
  14.                     //--Initalise the size of the array
  15.                     byte[] file = new byte[imageLoad2.InputStream.Length];
  16.  
  17.                     //--Create a new BinaryReader and set the InputStream for the Images InputStream to the
  18.                     //--beginning, as we create the img using a stream.
  19.                     BinaryReader reader = new BinaryReader(imageLoad2.InputStream);
  20.                     imageLoad2.InputStream.Seek(0, SeekOrigin.Begin);
  21.  
  22.                     //--Load the image binary.
  23.                     file = reader.ReadBytes((int)imageLoad2.InputStream.Length);
  24.  
  25.                     //--Create a new image to be added to the database
  26.                     Avatar.id = Guid.NewGuid();
  27.                     Avatar.TableLink = LedgerUser.id;
  28.                     Avatar.RecordStatus = " ";
  29.                     Avatar.FileSize = imageLoad2.ContentLength;
  30.                     Avatar.FileName = imageLoad2.FileName;
  31.                     Avatar.FileSource = file;
  32.                     Avatar.FileContentType = imageLoad2.ContentType;
  33.                     Avatar.FileHeight = img.Height;
  34.                     Avatar.FileWidth = img.Width;
  35.  
  36.                     //-- Now we create the thumbnail and save it.
  37.                     if (Thumbnail == true)
  38.                     {
  39.                         byte[] thumbnail = Images.CreateThumbnailToByte(imageLoad2.InputStream, 100, 100);
  40.                         Avatar.ThumbnailSource = thumbnail;
  41.                         Avatar.ThumbnailFileSize = thumbnail.Length;
  42.                         Avatar.ThumbnailContentType = Files.GetContentType(imageLoad2.FileName);
  43.                         Avatar.ThumbnailHeight = Images.FromByteHeight(thumbnail);
  44.                         Avatar.ThumbnailWidth = Images.FromByteWidth(thumbnail);
  45.                     }
  46.                     else
  47.                     {
  48.                         byte[] thumbnail = new byte[0];
  49.                         Avatar.ThumbnailSource = thumbnail;
  50.                         Avatar.ThumbnailFileSize = 0;
  51.                         Avatar.ThumbnailContentType = " ";
  52.                         Avatar.ThumbnailHeight = 0;
  53.                         Avatar.ThumbnailWidth = 0;
  54.  
  55.                     }
  56.                 }
  57.             }
  58.  
  59.             if (!ModelState.IsValid)
  60.             {
  61.                 ModelState.ModelStateErrors();
  62.             }
  63.             if (ModelState.IsValid)
  64.             {
  65.                 using (AccountsEntities context = new AccountsEntities())
  66.                 {
  67.                     try
  68.                     {
  69.                         //--Save the LedgerUser
  70.                         context.LedgerUsers.AddObject(LedgerUser);
  71.                         context.SaveChanges();
  72.                         //--Save the Image
  73.                         context.Images.AddObject(Avatar);
  74.                         context.SaveChanges();
  75.                         return RedirectToAction("Index", "Home");
  76.                     }
  77.                     catch (OptimisticConcurrencyException)
  78.                     {
  79.                         context.Refresh(RefreshMode.ClientWins, LedgerUser);
  80.                         context.SaveChanges();
  81.                         Console.WriteLine("OptimisticConcurrencyException " + "handled and changes saved");
  82.                     }
  83.                     catch (UpdateException ex)
  84.                     {
  85.                         Console.WriteLine(ex.ToString());
  86.                     }
  87.                 }
  88.             }
  89.  
  90.             var userTypes = new SelectList(db.UserTypes, "id", "Description");
  91.             var ledgerUser = new LedgerUser()
  92.             {
  93.                 id = LedgerUser.id,
  94.                 RecordStatus = LedgerUser.RecordStatus,
  95.                 CreatedDate = LedgerUser.CreatedDate,
  96.                 DateOfBirth = LedgerUser.DateOfBirth
  97.             };
  98.  
  99.             var viewModel = new LedgerUserViewModel()
  100.             {
  101.                 UserTypes = userTypes,
  102.                 LedgerUser = ledgerUser
  103.             };
  104.  
  105.             return View(viewModel);
  106.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement