Intranet-FAQ / IntranetFAQ.Web / Controllers / FaqController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using IntranetFAQ.Models;
using IntranetFAQ.Models.Entities;

namespace IntranetFAQ.Web.Controllers
{
    public class FaqController : Controller
    {
        private static FaqContext faqContext = new FaqContext();
        //
        // GET: /Faq/

        public ActionResult Index()
        {
            var faqList = from faq in faqContext.Faqs
                          orderby faq.LastModified descending
                          select faq;
            return View(faqList.ToList<Faq>());
        }

        //
        // GET: /Faq/Details/5

        public ActionResult Details(int id)
        {
            var faq = from item in faqContext.Faqs
                    where item.Id == id
                    select item;
            return View(faq.Single<Faq>());
        }

        //
        // GET: /Faq/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Faq/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                Faq faq = faqContext.Faqs.Create<Faq>();
                faq.Title = collection["Title"];
                faq.Content = collection["Content"];
                faq.UserName = User.Identity.Name;
                faq.LastModified = DateTime.Now;

                faqContext.Entry(faq).State = System.Data.EntityState.Added;

                faqContext.SaveChanges();

                return RedirectToAction("Index", "Faq");
            }
            catch (Exception ex)
            {
                return View(ex);
            }
        }

        //
        // GET: /Faq/Edit/5

        public ActionResult Edit(int id)
        {
            var faq = from item in faqContext.Faqs
                      where item.Id == id
                      select item;
            return View(faq.Single<Faq>());
        }

        //
        // POST: /Faq/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                var faq = (from item in faqContext.Faqs
                           where item.Id == id
                           select item).Single<Faq>();
                
                faq.Title = collection["Title"];
                faq.Content = collection["Content"];
                faq.LastModified = DateTime.Now;

                faqContext.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Faq/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Faq/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.