コード例 #1
0
 public ActionResult Index(string entryId)
 {
     using (RowanHouseDb db = new RowanHouseDb())
     {
         var viewModel = new BlogEntriesIndexViewModel() {
             BlogEntryList = db.BlogEntries.OrderByDescending(e => e.DatePublished).ToList(),
             SelectedID = entryId
         };
         if (!string.IsNullOrEmpty(entryId))
         {
             var page = db.BlogEntries.FirstOrDefault(e => e.ID.ToString() == entryId);
             viewModel.Title = page.Title;
             viewModel.Url = page.Url;
             viewModel.Content = page.Content;
             viewModel.ParsedContent =_markdown.Transform(viewModel.Content);
         }
         return View("Index", viewModel);
     }
 }
コード例 #2
0
        public ActionResult Index(BlogEntriesIndexViewModel viewModel)
        {
            using (RowanHouseDb db = new RowanHouseDb())
            {
                viewModel.BlogEntryList = db.BlogEntries.OrderByDescending(e => e.DatePublished).ToList();
                if (!ModelState.IsValid)
                {
                    return View("Index", viewModel);
                }

                BlogEntry entry = null;
                if (!string.IsNullOrEmpty(viewModel.SelectedID))
                {
                    entry = db.BlogEntries.First(e => e.ID.ToString() == viewModel.SelectedID);
                    entry.Title = viewModel.Title;
                    entry.Url = viewModel.Url;
                    entry.Content = viewModel.Content;
                    entry.Author = db.Users.FirstOrDefault(e => e.Username.ToLower().Equals(User.Identity.Name.ToLower()));
                    entry.DatePublished = DateTime.Now;
                    db.Entry(entry).State = System.Data.Entity.EntityState.Modified;
                }
                else
                {
                    entry = new BlogEntry() {
                        Title = viewModel.Title,
                        Url = viewModel.Url,
                        Content = viewModel.Content,
                        Author = db.Users.FirstOrDefault(e => e.Username.ToLower().Equals(User.Identity.Name.ToLower())),
                        DatePublished = DateTime.Now
                    };
                    db.BlogEntries.Add(entry);
                }
                db.SaveChanges();
                return RedirectToRoute("Blog_Entries", new { url = entry.Url });
            }
        }