コード例 #1
0
ファイル: BlogController.cs プロジェクト: TheRealSplith/YHW
        public ActionResult New(NewBlogVM vm)
        {
            Blog b = new Blog();
            using (var context = new SocialContext())
            {
                b.BlogText = vm.BlogText;
                b.Title = vm.Title;
                // calculate SubText
                int textLength = Math.Min(100, b.BlogText.Length);
                b.SubText = b.BlogText.Substring(0, textLength);
                if (textLength != b.BlogText.Length)
                    b.SubText += "...";

                b.Author = context.UserProfile.Where(u => u.UserName == User.Identity.Name).FirstOrDefault();
                if (b.Author == null)
                    return RedirectToAction("Home", "Index");

                b.CreatedDate = DateTime.Now;
                b.IsOpinion = vm.IsOpinion == "on";

                // ThumbImage
                if (vm.ThumbFile != null)
                {
                    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                    {
                        vm.ThumbFile.InputStream.CopyTo(ms);
                        byte[] array = ms.GetBuffer();

                        b.ThumbURL = array;
                    }
                }
                // Image
                if (vm.ImageFile != null)
                {
                    using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
                    {
                        vm.ImageFile.InputStream.CopyTo(ms);
                        byte[] array = ms.GetBuffer();

                        b.ImageURL = array;
                    }
                }

                context.BlogPost.Add(b);
                context.SaveChanges();
                return RedirectToAction("Item", "Blog", new { id = b.ID });
            }
        }
コード例 #2
0
        public void Post(Blog b)
        {
            if (b == null)
                return;

            using (var context = new YHW.Models.SocialContext())
            {
                try
                {
                    context.BlogPost.Add(b);
                    context.SaveChanges();
                }
                catch(Exception e)
                {
                    // Dive on error
                    Console.WriteLine(e.Message);
                }
            }
        }