Commits

Tairan Wang committed 3d13a06

improve the logic and bug fix.

Comments (0)

Files changed (9)

IntranetFAQ.Web/Controllers/FaqController.cs

 {
     public class FaqController : Controller
     {
-        private FaqContext faqContext = new FaqContext();
+        private static FaqContext faqContext = new FaqContext();
         //
         // GET: /Faq/
 
             try
             {
                 // TODO: Add insert logic here
-                Faq entry = new Faq()
-                {
-                    Guid = Guid.NewGuid(),
-                    UserName = User.Identity.Name,
-                    Title = collection["Title"],
-                    Content = collection["Content"],
-                    LastModified = DateTime.Now
-                };
+                Faq faq = faqContext.Faqs.Create<Faq>();
+                faq.Title = collection["Title"];
+                faq.Content = collection["Content"];
+                faq.UserName = User.Identity.Name;
+                faq.LastModified = DateTime.Now;
 
-                faqContext.Faqs.Add(entry);
+                faqContext.Entry(faq).State = System.Data.EntityState.Added;
+
                 faqContext.SaveChanges();
 
                 return RedirectToAction("Index", "Faq");
 
         public ActionResult Edit(int id)
         {
-            return View();
+            Faq faq = (from item in faqContext.Faqs
+                       where item.Id == id
+                       select item).First<Faq>();
+            return View(faq);
         }
 
         //
             try
             {
                 // TODO: Add update logic here
+                Faq faq = (from item in faqContext.Faqs
+                           where item.Id == id
+                           select item).First<Faq>();
+                faq.Title = collection["Title"];
+                faq.Content = collection["Content"];
+                faq.LastModified = DateTime.Now;
 
+                faqContext.SaveChanges();
                 return RedirectToAction("Index");
             }
             catch

IntranetFAQ.Web/Global.asax.cs

             RegisterGlobalFilters(GlobalFilters.Filters);
             RegisterRoutes(RouteTable.Routes);
 
-            Database.SetInitializer<FaqContext>(new DropCreateDatabaseAlways<FaqContext>());
+            Database.SetInitializer<FaqContext>(new DropCreateDatabaseIfModelChanges<FaqContext>());
         }
     }
 }

IntranetFAQ.Web/Models/Entities/Faq.cs

         [Key]
         public int Id { get; set; }
 
-        [DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]
-        public Guid Guid { get; set; }
-
-        [Required]
         public string UserName { get; set; }
 
         [Required]
         [Required]
         public string Content { get; set; }
 
-        [Required]
         public DateTime LastModified { get; set; }
     }
 

IntranetFAQ.Web/Models/FaqDatabaseInitializer.cs

 {
     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/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()) {
+@using (Html.BeginForm("Create", "Faq", FormMethod.Post, new { @class = "form-horizontal" }))
+{
     @Html.ValidationSummary(true)
     <fieldset>
         <legend>Faq</legend>
-
-        <div class="editor-label">
-            @Html.LabelFor(model => model.Guid)
+        <div class="control-group">
+            @Html.LabelFor(model => model.Title)
+            <div class="controls">
+                @Html.EditorFor(model => model.Title)
+                @Html.ValidationMessageFor(model => model.Title)
+            </div>
         </div>
-        <div class="editor-field">
-            @Html.EditorFor(model => model.Guid)
-            @Html.ValidationMessageFor(model => model.Guid)
+        <div class="control-group">
+            @Html.LabelFor(model => model.Content)
+            <div class="controls">
+                @Html.TextAreaFor(model => model.Content)
+                @Html.ValidationMessageFor(model => model.Content)
+            </div>
         </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>
     @Html.ActionLink("Back to List", "Index")
 </div>
+<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>

IntranetFAQ.Web/Views/Faq/Details.cshtml

 @model IntranetFAQ.Models.Entities.Faq
-
 @{
     ViewBag.Title = "Details";
     Layout = "~/Views/Shared/_Html5Boilerplate.cshtml";
 }
-
-<h2>Details</h2>
-
+<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-label">
+        UserName</div>
     <div class="display-field">
         @Html.DisplayFor(model => model.UserName)
     </div>
-
-    <div class="display-label">Title</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-label">
+        Content</div>
     <div class="display-field">
         @Html.DisplayFor(model => model.Content)
     </div>
-
-    <div class="display-label">LastModified</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("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()) {
+<h2>
+    Edit</h2>    
+@using (Html.BeginForm("Edit", "Faq", FormMethod.Post, new { @class = "form-horizontal" }))
+{
     @Html.ValidationSummary(true)
     <fieldset>
         <legend>Faq</legend>
-
         @Html.HiddenFor(model => model.Id)
-
-        <div class="editor-label">
-            @Html.LabelFor(model => model.Guid)
+        <div class="control-group">
+            @Html.LabelFor(model => model.UserName)
+            <div class="controls">
+                @Html.EditorFor(model => model.UserName)
+                @Html.ValidationMessageFor(model => model.UserName)
+            </div>
         </div>
-        <div class="editor-field">
-            @Html.EditorFor(model => model.Guid)
-            @Html.ValidationMessageFor(model => model.Guid)
+        <div class="control-group">
+            @Html.LabelFor(model => model.Title)
+            <div class="controls">
+                @Html.EditorFor(model => model.Title)
+                @Html.ValidationMessageFor(model => model.Title)
+            </div>
         </div>
-
-        <div class="editor-label">
-            @Html.LabelFor(model => model.UserName)
+        <div class="control-group">
+            @Html.LabelFor(model => model.Content)
+            <div class="controls">
+                @Html.TextAreaFor(model => model.Content)
+                @Html.ValidationMessageFor(model => model.Content)
+            </div>
         </div>
-        <div class="editor-field">
-            @Html.EditorFor(model => model.UserName)
-            @Html.ValidationMessageFor(model => model.UserName)
+        <div class="control-group">
+            @Html.LabelFor(model => model.LastModified)
+            <div class="controls">
+                @Html.EditorFor(model => model.LastModified)
+                @Html.ValidationMessageFor(model => model.LastModified)
+            </div>
         </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>
+<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>

IntranetFAQ.Web/Views/Faq/Index.cshtml

 @model IEnumerable<IntranetFAQ.Models.Entities.Faq>
-
 @{
     ViewBag.Title = "Index";
     Layout = "~/Views/Shared/_Html5Boilerplate.cshtml";
 }
-
-<h2>Index</h2>
-
+@*
 <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>
+</p>*@
+@foreach (var item in Model)
+{
+    <div style="padding:5px">
+        <h2>@Html.DisplayFor(modelItem => item.Title)</h2>
+        <p>
+            @Html.DisplayFor(modelItem => item.Content) 
+            @Html.ActionLink("read more...", "Details", new { id = item.Id })
+        </p>
+        <p>
+            Author: @Html.DisplayFor(modelItem => item.UserName) Last modified date: @Html.DisplayFor(modelItem => item.LastModified)</p>
+        @if (User.Identity.Name == item.UserName)
+        {
+            @Html.ActionLink("Edit", "Edit", new { id = item.Id })
+        }
+@*@Html.ActionLink("Delete", "Delete", new { id = item.Id })*@
+    </div>
+    <hr />
 }
-
-</table>

IntranetFAQ.Web/Views/Shared/_Html5Boilerplate.cshtml

     <link rel="stylesheet" href="@Url.Content("~/Content/style.css?v=2")">
     @*<link rel="stylesheet" media="handheld" href="@Url.Content("~/Content/handheld.css?v=2")">*@
     <script src="@Url.Content("~/Scripts/libs/modernizr-1.7.min.js")" type="text/javascript"></script>
+    <script src="@Url.Content("~/Scripts/jquery-1.6.4.min.js")" type="text/javascript"></script>
+    <script type="text/javascript">!window.jQuery && document.write(unescape('%3Cscript src="@Url.Content("~/Scripts/jquery-1.6.4.min.js")"%3E%3C/script%3E'))</script>
 </head>
 <body>
     <div>
 
 		</footer>
     </div>
-    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
-    <script type="text/javascript">        !window.jQuery && document.write(unescape('%3Cscript src="@Url.Content("~/Scripts/libs/jquery-1.5.1.min.js")"%3E%3C/script%3E'))</script>
+    
     <script src="@Url.Content("~/Scripts/plugins.js")" type="text/javascript"></script>
     <script src="@Url.Content("~/Scripts/script.js")" type="text/javascript"></script>
     <!--[if lt IE 7 ]>