一个封装较好的删除方法(Delete)
前台的引用 @Html.ActionLink(“删除字样”,“后台的删除方法”,new{绑定id},new{@style="样式"});方法,如何要独立使用的话,一般还要使用到相应的js方法:
<ul> @{ foreach (var item in Model) { string url = item.FUrl; <li> <div style="float: left;position:relative; width:100%"> <div style="float: left; position:relative;"> <span style="vertical-align:middle; "> <!-- 【2item.FClassifiedId】 --> 【@item.UserFavoTopic.FName】 @*@Html.ActionLink( item.FTitle.Length > 30? item.FTitle.Substring(0, 30) + "……" : item.FTitle, "List", @item.FUrl, new { articleId = item.FId }, new { @title = item.FTitle.Length > 30 ? item.FTitle : "", @target = "_blank" })*@ <a href="@url" target="_blank"> @item.FTitle</a> </span> </div> <div style="text-align: right; vertical-align:middle; float:right "> @* @Html.ActionLink("编辑", "Edit", new { id = item.FId}, new { @style = "vertical-align: middle",@class="edit" })*@ @* 【@p.UserFavoTopic.FName】<a href='@p.FUrl.Trim()'target="_blank"> @p.FTitle</a>*@ <a href="#" class="edit">编辑分类</a> <!--树状目录 2Html.LabelFor(m => Model.FOuthClassifiedId) --> @Html.ActionLink("删除", "Delete", new { id = item.FId }, new { @style = "vertical-align: middle" }) </div> </div> </li> } } </ul>
相应的js方法:(作用:根据相应的路径找到相应的方法)
<script type="text/javascript"> function ajaxRequest(params) { var randnum = Math.random(); var ajaxurl = params.url; var ifasync = params.ifasyncinre; if (ifasync != true || ifasync != false) { ifasync = false; } var re = null; var method = params.method; if (method == null || method == "") method = "POST"; var data = params.data; if (data == null || data == "") data = ""; var datatype = params.datatype; if (datatype == null || datatype == "") datatype = "json"; $.ajax({ type: method, url: ajaxurl, async: ifasync, data: data, dataType: datatype, success: function (result) { re = result; } }); return re; } $(".delete").live("click", function () { if (confirm("您确定要删除吗") == false) { return false; } debugger; var Fid = $(this).attr("Fid");
//删除方法的控制器; var params = { url: '/Personal/Space/Favorite/Delete', data: { id: Fid } }; var result = ajaxRequest(params); if (result.Data == "1") { // 当前页
location.href("/Personal/Space/Favorite/Index"); } else if (result.Data == "0") { alert(result.Message); } else { alert(result.Message); } }) </script>
后台的删除方法和默认的页面:(前台的js,调用相应的后台的删除方法和返回重定向页面)
[HttpGet] [SupportFilter] public ActionResult Delete(string id) { if (id != null) { if (ufb.Delete(id)) { return RedirectToAction("Index"); //因为是同一页面(当前页),所以重定向时,可以直接定义 为:“Index”,无需添加其他路径。
//return Json(new TipMessage { IsError = false, Message = "删除成功", Data = 1 }, JsonRequestBehavior.AllowGet); } else { //return Json(new TipMessage { IsError = false, Message = "删除失败", Data = 0 }, JsonRequestBehavior.AllowGet); //安全起见,可以在这里进行一个异常捕获。
} } return RedirectToAction("Index"); }
主页:(后台)
[HttpGet] [SupportFilter] public ActionResult Index(string id,int page=1,int pageSize=20) //UserFavorite model , UserFavoTopic model2 { //获资收藏信息 List<UserFavorite> list = (from d in db.UserFavorite select d).ToList(); List<UserFavoTopic> list2 = (from f in db.UserFavoTopic select f).ToList(); // 绑定收藏类型 //获得一级分类FName //获取二级分类FNmee UserFavorite uf = db.UserFavorite.Where( c => c.FUrl == id).FirstOrDefault<UserFavorite>(); //数据的绑定吧FID绑定到id上面的获取表数据(UserFavorite)的第一手资料 //下一步:将集合数据传给视图 ViewBag.DataList = list;// 这是第一种传值法,用ViewBag ViewBag.DataList2 = list2; // model.FFavorId = model2.FId; //model.FFavorId //IEnumerable<UserFavorite> list = ufb.FindALL(); // List<UserFavoTopic> uft = uftb.FindALL().ToList(); var pageList = list.Skip((page - 1) * pageSize).Take(pageSize).ToList(); var pageList2 = new PagedList<UserFavorite>(pageList, page, pageSize, pageList.Count()); return View(pageList2); //return View(uf); }
附加:(分页功能:-------------------------------)
<div style="float:left; position:relative; width:70%"> <div id="divData"> @{ //把 _ListByTable2 页面嵌套在当前页面, 在 _ListByTable2.cshtml页面中已经获得数据库中的表数据。
Html.RenderPartial("_ListByTable2", Model, this.ViewData); } </div> //具体的分页显示
<div class="pageBottom clear_box snPages"> <div class="pagel"> @Html.PageSizeHtml() 共找到 <strong class="col_r">@Model.TotalItemCount </strong>条数据 </div> <div class="pager"> @Html.Pager(Model, new PagerOptions { NumericPagerItemCount = 8, ShowMorePagerItems = true, ShowFirstLast = false, PageIndexParameterName = "page", CurrentPagerItemWrapperFormatString = "<span class=\"cpb\">{0}</span>", NumericPagerItemWrapperFormatString = "<span class=\"item\">{0}</span>", AlwaysShowFirstLastPageNumber = true }, "PersonalSpace_default", null) </div> <div class="clear"> </div> </div> </div>
有些梦想是遥不可及的,除非你坚持!