Commits

Tairan Wang committed 24f0bc6

uha, it works.

Comments (0)

Files changed (23)

 obj
 bin
 *.suo
+*.user
 TestResults

IntranetFAQ.Web/Controllers/FaqController.cs

 using System.Web;
 using System.Web.Mvc;
 using IntranetFAQ.Models;
+using IntranetFAQ.Models.Entities;
 
 namespace IntranetFAQ.Web.Controllers
 {
     public class FaqController : Controller
     {
-        private FaqDbContext DbContext = new FaqDbContext();
+        private FaqContext faqContext = new FaqContext();
         //
         // GET: /Faq/
 
         public ActionResult Index()
         {
-            return View(DbContext.Faqs.ToList<Faq>());
+            List<Faq> faqList = faqContext.Faqs.ToList<Faq>();
+            return View(faqList);
         }
 
         //
 
         public ActionResult Details(int id)
         {
-            return View();
+            var f = (from faq in faqContext.Faqs
+                       where faq.Id == id
+                       select faq).First<Faq>();
+            return View(f);
         }
 
         //
         public ActionResult Create()
         {
             return View();
-        } 
+        }
 
         //
         // POST: /Faq/Create
             try
             {
                 // TODO: Add insert logic here
-                Faq entry = new Faq() {
+                Faq entry = new Faq()
+                {
+                    Guid = Guid.NewGuid(),
                     UserName = User.Identity.Name,
-                    Title = collection["id_title"],
-                    Content = collection["id_content"],
-                    LastModified = DateTime.Now };
-                DbContext.Faqs.Add(entry);
-                DbContext.SaveChanges();
-                return RedirectToAction("Index");
+                    Title = collection["Title"],
+                    Content = collection["Content"],
+                    LastModified = DateTime.Now
+                };
+
+                faqContext.Faqs.Add(entry);
+                faqContext.SaveChanges();
+
+                return RedirectToAction("Index", "Faq");
             }
-            catch(Exception ex)
+            catch (Exception ex)
             {
                 return View(ex);
             }
         }
-        
+
         //
         // GET: /Faq/Edit/5
- 
+
         public ActionResult Edit(int id)
         {
             return View();
             try
             {
                 // TODO: Add update logic here
- 
+
                 return RedirectToAction("Index");
             }
             catch
 
         //
         // GET: /Faq/Delete/5
- 
+
         public ActionResult Delete(int id)
         {
             return View();
             try
             {
                 // TODO: Add delete logic here
- 
+
                 return RedirectToAction("Index");
             }
             catch

IntranetFAQ.Web/Controllers/HomeController.cs

-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Web;
-using System.Web.Mvc;
-
-namespace IntranetFAQ.Web.Controllers
-{
-    public class HomeController : Controller
-    {
-        public ActionResult Index()
-        {
-            ViewBag.Message = "Welcome to ASP.NET MVC!";
-
-            return View();
-        }
-
-        public ActionResult About()
-        {
-            return View();
-        }
-    }
-}

IntranetFAQ.Web/Global.asax.cs

             routes.MapRoute(
                 "Default", // Route name
                 "{controller}/{action}/{id}", // URL with parameters
-                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
+                new { controller = "Faq", action = "Index", id = UrlParameter.Optional } // Parameter defaults
             );
 
         }
             RegisterGlobalFilters(GlobalFilters.Filters);
             RegisterRoutes(RouteTable.Routes);
 
-            Database.SetInitializer<FaqDbContext>(new DbInitializer());
+            Database.SetInitializer<FaqContext>(new DropCreateDatabaseAlways<FaqContext>());
         }
     }
 }

IntranetFAQ.Web/IntranetFAQ.Web.csproj

   </ItemGroup>
   <ItemGroup>
     <Compile Include="Controllers\FaqController.cs" />
-    <Compile Include="Controllers\HomeController.cs" />
     <Compile Include="Global.asax.cs">
       <DependentUpon>Global.asax</DependentUpon>
     </Compile>
-    <Compile Include="Models\DbInitializer.cs" />
-    <Compile Include="Models\Faq.cs" />
-    <Compile Include="Models\FaqDbContext.cs" />
+    <Compile Include="Models\FaqDatabaseInitializer.cs" />
+    <Compile Include="Models\Entities\Faq.cs" />
+    <Compile Include="Models\FaqContext.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
   </ItemGroup>
   <ItemGroup>
     <Content Include="apple-touch-icon-72x72-precomposed.png" />
     <Content Include="apple-touch-icon-precomposed.png" />
     <Content Include="apple-touch-icon.png" />
-    <Content Include="App_Data\IntranetFAQ.mdf" />
-    <Content Include="App_Data\IntranetFAQ_log.ldf">
-      <DependentUpon>IntranetFAQ.mdf</DependentUpon>
-    </Content>
     <Content Include="Content\bootstrap.css" />
     <Content Include="Content\bootstrap.min.css" />
     <Content Include="Content\bootstrap.min.responsive.css" />
     <Content Include="Scripts\MicrosoftMvcValidation.js" />
     <Content Include="Scripts\MicrosoftMvcValidation.debug.js" />
     <Content Include="Views\_ViewStart.cshtml" />
-    <Content Include="Views\Home\About.cshtml" />
-    <Content Include="Views\Home\Index.cshtml" />
     <Content Include="Views\Shared\Error.cshtml" />
     <Content Include="Views\Shared\_Layout.cshtml" />
     <Content Include="Views\Web.config" />
     <Content Include="packages.config" />
     <None Include="readme.txt" />
     <Content Include="Views\Shared\_Html5Boilerplate.cshtml" />
+    <Content Include="Views\Faq\Details.cshtml" />
+    <Content Include="Views\Faq\Index.cshtml" />
     <Content Include="Views\Faq\Create.cshtml" />
-    <Content Include="Views\Faq\Index.cshtml" />
+    <Content Include="Views\Faq\Edit.cshtml" />
   </ItemGroup>
-  <ItemGroup />
+  <ItemGroup>
+    <Folder Include="App_Data\" />
+  </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 

IntranetFAQ.Web/Models/DbInitializer.cs

-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Web;
-using System.Data.Entity;
-using System.Data.Entity.Infrastructure;
-using IntranetFAQ.Models;
-
-public class DbInitializer : CreateDatabaseIfNotExists<FaqDbContext>
-{
-    protected override void Seed(FaqDbContext context)
-    {
-        base.Seed(context);
-    }
-}

IntranetFAQ.Web/Models/Entities/Faq.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.ComponentModel.DataAnnotations;
+
+namespace IntranetFAQ.Models.Entities
+{
+    public class Faq
+    {
+        [Key]
+        public int Id { get; set; }
+
+        [DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]
+        public Guid Guid { get; set; }
+
+        [Required]
+        public string UserName { get; set; }
+
+        [Required]
+        public string Title { get; set; }
+
+        [Required]
+        public string Content { get; set; }
+
+        [Required]
+        public DateTime LastModified { get; set; }
+    }
+
+    //public class Comment
+    //{
+    //    public Guid Guid { get; set; }
+    //}
+}

IntranetFAQ.Web/Models/Faq.cs

-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.ComponentModel.DataAnnotations;
-
-namespace IntranetFAQ.Models
-{
-    public class Faq
-    {
-        [Key]
-        public Guid GUID { get; set; }
-        
-        public string UserName { get; set; }
-
-        public string Title { get; set; }
-
-        public string Content { get; set; }
-
-        public DateTime LastModified { get; set; }
-    }
-
-    public class Comment
-    {
-        public Guid GUID { get; set; }
-    }
-}

IntranetFAQ.Web/Models/FaqContext.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Data.Entity;
+using IntranetFAQ.Models.Entities;
+
+namespace IntranetFAQ.Models
+{
+    public class FaqContext : DbContext
+    {
+        public FaqContext()
+            : base("ApplicationServices")
+        {
+        }
+
+        public DbSet<Faq> Faqs { get; set; }
+    }
+}

IntranetFAQ.Web/Models/FaqDatabaseInitializer.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+using System.Data.Entity;
+using System.Data.Entity.Infrastructure;
+using IntranetFAQ.Models;
+using IntranetFAQ.Models.Entities;
+
+public class FaqDatabaseInitializer : DropCreateDatabaseIfModelChanges<FaqContext>
+{
+    protected override void Seed(FaqContext context)
+    {
+        Faq faq1 = new Faq()
+        {
+            Guid = Guid.NewGuid(),
+            Title = "test Faq",
+            Content = "Faq content",
+            UserName = "",
+            LastModified = DateTime.Now
+        };
+
+        context.Faqs.Add(faq1);
+
+        base.Seed(context);
+    }
+}

IntranetFAQ.Web/Models/FaqDbContext.cs

-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Data.Entity;
-
-namespace IntranetFAQ.Models
-{
-    public class FaqDbContext : DbContext
-    {
-        public FaqDbContext()
-            : base("ApplicationServices")
-        {
-        }
-
-        public DbSet<Faq> Faqs { get; set; }
-    }
-}

IntranetFAQ.Web/Views/Faq/Create.cshtml

-@{
+@model IntranetFAQ.Models.Entities.Faq
+
+@{
     ViewBag.Title = "Create";
     Layout = "~/Views/Shared/_Html5Boilerplate.cshtml";
 }
+
+<h2>Create</h2>
+
+<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
+<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
+
+@using (Html.BeginForm()) {
+    @Html.ValidationSummary(true)
+    <fieldset>
+        <legend>Faq</legend>
+
+        <div class="editor-label">
+            @Html.LabelFor(model => model.Guid)
+        </div>
+        <div class="editor-field">
+            @Html.EditorFor(model => model.Guid)
+            @Html.ValidationMessageFor(model => model.Guid)
+        </div>
+
+        <div class="editor-label">
+            @Html.LabelFor(model => model.UserName)
+        </div>
+        <div class="editor-field">
+            @Html.EditorFor(model => model.UserName)
+            @Html.ValidationMessageFor(model => model.UserName)
+        </div>
+
+        <div class="editor-label">
+            @Html.LabelFor(model => model.Title)
+        </div>
+        <div class="editor-field">
+            @Html.EditorFor(model => model.Title)
+            @Html.ValidationMessageFor(model => model.Title)
+        </div>
+
+        <div class="editor-label">
+            @Html.LabelFor(model => model.Content)
+        </div>
+        <div class="editor-field">
+            @Html.EditorFor(model => model.Content)
+            @Html.ValidationMessageFor(model => model.Content)
+        </div>
+
+        <div class="editor-label">
+            @Html.LabelFor(model => model.LastModified)
+        </div>
+        <div class="editor-field">
+            @Html.EditorFor(model => model.LastModified)
+            @Html.ValidationMessageFor(model => model.LastModified)
+        </div>
+
+        <p>
+            <input type="submit" value="Create" />
+        </p>
+    </fieldset>
+}
+
 <div>
-    <form action="/Faq/Create" method="post" class="form-horizontal">
-    <div class="control-group">
-        <label class="control-label" for="input01">
-            Question</label>
-        <div class="controls">
-            <input type="text" class="input-xlarge" id="id_title">
-        </div>
-    </div>
-    <div class="control-group">
-        <label class="control-label" for="input01">
-            Answer</label>
-        <div class="controls">
-            <textarea id="id_content"></textarea>
-        </div>
-    </div>
-    <div>
-        <button type="submit" class="btn btn-primary">
-            Save</button>
-    </div>
-    </form>
+    @Html.ActionLink("Back to List", "Index")
 </div>

IntranetFAQ.Web/Views/Faq/Details.cshtml

+@model IntranetFAQ.Models.Entities.Faq
+
+@{
+    ViewBag.Title = "Details";
+    Layout = "~/Views/Shared/_Html5Boilerplate.cshtml";
+}
+
+<h2>Details</h2>
+
+<fieldset>
+    <legend>Faq</legend>
+
+    <div class="display-label">Guid</div>
+    <div class="display-field">
+        @Html.DisplayFor(model => model.Guid)
+    </div>
+
+    <div class="display-label">UserName</div>
+    <div class="display-field">
+        @Html.DisplayFor(model => model.UserName)
+    </div>
+
+    <div class="display-label">Title</div>
+    <div class="display-field">
+        @Html.DisplayFor(model => model.Title)
+    </div>
+
+    <div class="display-label">Content</div>
+    <div class="display-field">
+        @Html.DisplayFor(model => model.Content)
+    </div>
+
+    <div class="display-label">LastModified</div>
+    <div class="display-field">
+        @Html.DisplayFor(model => model.LastModified)
+    </div>
+</fieldset>
+<p>
+    @Html.ActionLink("Edit", "Edit", new { id=Model.Id }) |
+    @Html.ActionLink("Back to List", "Index")
+</p>

IntranetFAQ.Web/Views/Faq/Edit.cshtml

+@model IntranetFAQ.Models.Entities.Faq
+
+@{
+    ViewBag.Title = "Edit";
+    Layout = "~/Views/Shared/_Html5Boilerplate.cshtml";
+}
+
+<h2>Edit</h2>
+
+<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
+<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
+
+@using (Html.BeginForm()) {
+    @Html.ValidationSummary(true)
+    <fieldset>
+        <legend>Faq</legend>
+
+        @Html.HiddenFor(model => model.Id)
+
+        <div class="editor-label">
+            @Html.LabelFor(model => model.Guid)
+        </div>
+        <div class="editor-field">
+            @Html.EditorFor(model => model.Guid)
+            @Html.ValidationMessageFor(model => model.Guid)
+        </div>
+
+        <div class="editor-label">
+            @Html.LabelFor(model => model.UserName)
+        </div>
+        <div class="editor-field">
+            @Html.EditorFor(model => model.UserName)
+            @Html.ValidationMessageFor(model => model.UserName)
+        </div>
+
+        <div class="editor-label">
+            @Html.LabelFor(model => model.Title)
+        </div>
+        <div class="editor-field">
+            @Html.EditorFor(model => model.Title)
+            @Html.ValidationMessageFor(model => model.Title)
+        </div>
+
+        <div class="editor-label">
+            @Html.LabelFor(model => model.Content)
+        </div>
+        <div class="editor-field">
+            @Html.EditorFor(model => model.Content)
+            @Html.ValidationMessageFor(model => model.Content)
+        </div>
+
+        <div class="editor-label">
+            @Html.LabelFor(model => model.LastModified)
+        </div>
+        <div class="editor-field">
+            @Html.EditorFor(model => model.LastModified)
+            @Html.ValidationMessageFor(model => model.LastModified)
+        </div>
+
+        <p>
+            <input type="submit" value="Save" />
+        </p>
+    </fieldset>
+}
+
+<div>
+    @Html.ActionLink("Back to List", "Index")
+</div>

IntranetFAQ.Web/Views/Faq/Index.cshtml

-@model IntranetFAQ.Models.Faq
+@model IEnumerable<IntranetFAQ.Models.Entities.Faq>
 
 @{
     ViewBag.Title = "Index";
 }
 
 <h2>Index</h2>
-@Html.DisplayForModel()
+
+<p>
+    @Html.ActionLink("Create New", "Create")
+</p>
+<table>
+    <tr>
+        <th>
+            Guid
+        </th>
+        <th>
+            UserName
+        </th>
+        <th>
+            Title
+        </th>
+        <th>
+            Content
+        </th>
+        <th>
+            LastModified
+        </th>
+        <th></th>
+    </tr>
+
+@foreach (var item in Model) {
+    <tr>
+        <td>
+            @Html.DisplayFor(modelItem => item.Guid)
+        </td>
+        <td>
+            @Html.DisplayFor(modelItem => item.UserName)
+        </td>
+        <td>
+            @Html.DisplayFor(modelItem => item.Title)
+        </td>
+        <td>
+            @Html.DisplayFor(modelItem => item.Content)
+        </td>
+        <td>
+            @Html.DisplayFor(modelItem => item.LastModified)
+        </td>
+        <td>
+            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
+            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
+            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
+        </td>
+    </tr>
+}
+
+</table>

IntranetFAQ.Web/Views/Home/About.cshtml

-@{
-    ViewBag.Title = "About Us";
-}
-
-<h2>About</h2>
-<p>
-     Put content here.
-</p>

IntranetFAQ.Web/Views/Home/Index.cshtml

-@{
-    ViewBag.Title = "Home Page";
-
-    Layout = "~/Views/Shared/_Html5Boilerplate.cshtml";
-}
-
-<h2>@ViewBag.Message</h2>
-<p>
-    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
-</p>

IntranetFAQ.Web/Views/Shared/_Html5Boilerplate.cshtml

     <script src="@Url.Content("~/Scripts/libs/modernizr-1.7.min.js")" type="text/javascript"></script>
 </head>
 <body>
-    <div id="container">
-        <header>
+    <div>
+        <header class="container">
             <div id="title">
                 <h1>Frequently Asked Questions</h1>
             </div>
             </div>
 
             <div id="menucontainer">
-
                 <ul id="menu">
-                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
+                    <li>@Html.ActionLink("Home", "Index", "Faq")</li>
                     <li>@Html.ActionLink("Create", "Create", "Faq")</li>
                 </ul>
             </div>
 		</header>
-        <div id="main" role="main">
+        <div class="container" style="background-color:White">
             @RenderBody()
         </div>
         <footer>

IntranetFAQ.Web/Web.config

   -->
 <configuration>
   <connectionStrings>
-    <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|IntranetFAQ.mdf;User Instance=true"
+    <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Initial Catalog=IntranetFAQ;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|IntranetFAQ.mdf;User Instance=true"
          providerName="System.Data.SqlClient" />
   </connectionStrings>
   <appSettings>