asp.net mvc3下 checkbox多選

來源:互聯網
上載者:User

//頁面

@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
    &nbsp;
    @if (Model.HasPreviousPage)
    {
        @Html.ActionLink("<<", "PostManage", new { page = 1, currentFilter = ViewBag.CurrentFilter })
        @Html.Raw("&nbsp;");
        @Html.ActionLink("< 上一頁", "PostManage", new { page = Model.PageNumber - 1, currentFilter = ViewBag.CurrentFilter })
    }
    else
    {
        @:<<
        @Html.Raw("&nbsp;");
        @:< 上一頁
    }
    &nbsp;
    @if (Model.HasNextPage)
    {
        @Html.ActionLink("下一頁 >", "PostManage", new { page = Model.PageNumber + 1, currentFilter = ViewBag.CurrentFilter })
        @Html.Raw("&nbsp;");
        @Html.ActionLink(">>", "PostManage", new { page = Model.PageCount, currentFilter = ViewBag.CurrentFilter })
    }
    else
    {
        @:下一頁 >
        @Html.Raw("&nbsp;")
        @:>>
    }
</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);
        }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.