Add "edit page" button to wiki

This commit is contained in:
Michael 2017-04-07 09:57:24 -04:00
parent 6989516207
commit 5860170c1f
5 changed files with 82 additions and 0 deletions

View file

@ -22,6 +22,50 @@ public ActionResult Index(string id = "", bool triedtolikeowntopic=false)
return View(model);
}
[Authorize]
public ActionResult EditPage(string id)
{
var db = new ApplicationDbContext();
var model = new AddWikiPageViewModel();
var wiki = db.WikiPages.FirstOrDefault(x => x.Id == id);
if (wiki == null)
return new HttpStatusCodeResult(404);
model.Content = wiki.Contents;
model.Name = wiki.Name;
model.ParentId = wiki.CategoryId;
model.PageId = id;
return View(model);
}
[Authorize]
[ValidateAntiForgeryToken]
[HttpPost]
public ActionResult EditPage(AddWikiPageViewModel model)
{
if (!ModelState.IsValid)
return View(model);
var db = new ApplicationDbContext();
var wiki = db.WikiPages.FirstOrDefault(x => x.Id == model.PageId);
if (wiki == null)
return new HttpStatusCodeResult(404);
var edit = new ForumPostEdit();
edit.PreviousState = wiki.Contents;
edit.EditedAt = DateTime.Now;
edit.UserId = User.Identity.GetUserId();
edit.Id = Guid.NewGuid().ToString();
edit.Parent = wiki.Id;
db.ForumPostEdits.Add(edit);
wiki.Contents = model.Content;
wiki.CategoryId = model.ParentId;
db.SaveChanges();
return RedirectToAction("Index", new { id = model.PageId });
}
public ActionResult Random()
{
var db = new ApplicationDbContext();

View file

@ -16,6 +16,7 @@ public AddWikiPageViewModel() : base()
[AllowHtml]
[Required(ErrorMessage = "Please enter content for your page.")]
public string Content { get; set; }
public string PageId { get; set; }
}
public class AddWikiCategoryViewModel

View file

@ -546,6 +546,7 @@
<Content Include="Views\Developer\Wiki.cshtml" />
<Content Include="Views\Developer\AddWikiCategory.cshtml" />
<Content Include="Views\Wiki\AddPage.cshtml" />
<Content Include="Views\Wiki\EditPage.cshtml" />
</ItemGroup>
<ItemGroup>
<Folder Include="App_Data\" />

View file

@ -0,0 +1,29 @@
@model Project_Unite.Models.AddWikiPageViewModel
@{
ViewBag.Title = "Edit page";
}
<h2>Edit @Model.Name</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(Model=>Model.PageId)
@Html.HiddenFor(Model=>Model.Name)
<table class="table">
<tr>
<td style="width:35%">Category:</td>
<td>@Html.DropDownListFor(Model=>Model.ParentId, Model.Parents, new{@class="form-control"})</td>
</tr>
<tr>
<td>Contents:</td>
<td>@Html.TextAreaFor(Model=>Model.Content, new{@class="form-control", rows="10"})</td>
</tr>
<tr>
<td></td>
<td><input type="submit" class="btn btn-primary" value="Edit" /></td>
</tr>
</table>
}

View file

@ -81,6 +81,13 @@
<p><strong>Last edited by @Html.UserLink(edit.UserId) on @edit.EditedAt</strong></p>
if (Request.IsAuthenticated)
{
<ul>
<li><a href="@Url.Action("EditPage", new { id = Model.Page.Id })"><span class="glyphicon glyphicon-pencil"></span> Edit this page</a></li>
</ul>
}
<p>@Html.Markdown(Model.Page.Contents)</p>
if (Request.IsAuthenticated)