//頁面
@model PagedList.IPagedList<TaoNiuBBS.Models.TN_Post>
@{
ViewBag.Title = "PostManage";
Layout = "~/Views/Shared/layout.cshtml";
}
<script src="../../Scripts/jquery-1.5.1.js" type="text/javascript"></script>
<script type="text/javascript">
<!-- Begin
var checkflag = "false";
function check(field) {
if (checkflag == "false") {
for (i = 0; i < field.length; i++) {
field[i].checked = true;
}
checkflag = "true";
return "反選";
}
else {
for (i = 0; i < field.length; i++) {
field[i].checked = false;
}
checkflag = "false";
return "全選";
}
}
// End -->
$(function () {
$("#DeleteList").click(function () {
var innertext = "";
$("input[type='checkbox']").each(function () {
if ($(this).attr("checked") == true) {
innertext += $(this).siblings("input[type='hidden']").val() + "|";
}
});
if (innertext != "") {
$.post("/Post/Delete?id=" + innertext, function () {
alert("刪除成功!");
});
}
});
})
</script>
<h2>PostManage</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<form name= "myform" method="post">
<table>
<tr>
<th>
選擇
</th>
<th>
文章標題
</th>
<th>
社區
</th>
<th>
使用者名稱
</th>
<th>
發布時間
</th>
<th>
附件
</th>
<th>
文章類型
</th>
<th>
查看
</th>
<th>
回複
</th>
<th>
最後回帖
</th>
<th>
是否已回複
</th>
<th>
是否精華
</th>
<th>
是否回收
</th>
<th>
操作
</th>
</tr>
@foreach (var item in Model) {
string status = "";
if (item.Status == 1)
{
status = "是";
}
else
{
status = "否";
}
string IsCream = "";
if (item.IsCream == true)
{
IsCream = "精華";
}
else
{
IsCream = "非精華";
}
<tr>
<td ><input type="checkbox" id="list" name="list" /><input type="hidden" value="@item.PostID" /></td>
<td>
@item.PostTitle
</td>
<td>
@item.TN_Community.CommunityName
</td>
<td>
@item.TN_UserInfo.UserID
</td>
<td>
@String.Format("{0:g}", item.PubTime)
</td>
<td>
<a href="@item.PostAttachment">查看</a>
</td>
<td>
@item.PostType.TypeName
</td>
<td>
@item.Views
</td>
<td>
@item.Reply
</td>
<td>
@String.Format("{0:g}", item.LastReplyTime)
</td>
<td>
@status
</td>
<td>
@IsCream
</td>
<td>
@item.IsMove
</td>
<td>
@Html.ActionLink("更改", "Edit", new { id=item.PostID }) |
@Html.ActionLink("查看", "Details", new { id=item.PostID })
</td>
</tr>
}
</table>
<input type="button" value="全選" onclick="this.value=check(this.form.list)"/>
<input type="submit" id="DeleteList" value="刪除" />
</form>
<div>
@(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
/ @Model.PageCount
@if (Model.HasPreviousPage)
{
@Html.ActionLink("<<", "PostManage", new { page = 1, currentFilter = ViewBag.CurrentFilter })
@Html.Raw(" ");
@Html.ActionLink("< 上一頁", "PostManage", new { page = Model.PageNumber - 1, currentFilter = ViewBag.CurrentFilter })
}
else
{
@:<<
@Html.Raw(" ");
@:< 上一頁
}
@if (Model.HasNextPage)
{
@Html.ActionLink("下一頁 >", "PostManage", new { page = Model.PageNumber + 1, currentFilter = ViewBag.CurrentFilter })
@Html.Raw(" ");
@Html.ActionLink(">>", "PostManage", new { page = Model.PageCount, currentFilter = ViewBag.CurrentFilter })
}
else
{
@:下一頁 >
@Html.Raw(" ")
@:>>
}
</div>
cotroller:
public ViewResult PostManage(string currentFilter, string searchString, int? page)
{
if (Request.HttpMethod == "GET")
{
searchString = currentFilter;
}
else
{
page = 1;
}
ViewBag.CurrentFilter = searchString;
var TN_Post = db.TN_Post.Where(c => c.IsMove == false).OrderByDescending(p => p.LastReplyTime).ToList();
if (!string.IsNullOrEmpty(searchString))
{
TN_Post = TN_Post.Where(e => e.PostTitle.Contains(searchString)).ToList();
}
int pageSize = 10;
int pageIndex = (page ?? 1) - 1;
return View(TN_Post.ToPagedList(pageIndex, pageSize));
}
//普通刪除
public ActionResult Delete(Guid id)
{
TN_Post tn_post = db.TN_Post.Find(id);
return View(tn_post);
}
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(string id)
{
if (id != null)
{
string[] str = id.Split('|');
foreach (var i in str)
{
if (i != "")
{
Guid postid = Guid.Parse(i.ToString());
TN_Post tn_post = db.TN_Post.Find(postid);
tn_post.IsMove = true;
UpdateModel(tn_post);
db.SaveChanges();
}
else
{
return RedirectToAction("Index");
}
}
}
else
{
return RedirectToAction("Index");
}
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}