private static string getLinkHtml(UrlHelper urlHelper, bool useAjax, string ajaxSuccessFunction, string linkContent, string actionName, string controllerName, RouteValueDictionary routeValues) { string link = ""; if (useAjax) { link += "<a href="javascript:void(0);" onclick="javascript:$.post('" + urlHelper.Action(actionName, controllerName) + "',{"; //将route放到post表单中 foreach (var route in routeValues.Keys) { link += route + ":'" + routeValues[route].ToString() + "',"; } if (routeValues.Count > 0) { link = link.Remove(link.Length - 1); } link += "}," + ajaxSuccessFunction + ")" >"; } else { link += "<a href="" + urlHelper.Action(actionName, controllerName, routeValues) + "">"; } link += linkContent; link += "</a>"; return link; }
[HttpPost] public ActionResult GoPage() { int pageSize = 4; int allCount = db.Movies.Count(); ViewBag.Num = allCount; ViewBag.PageSize = pageSize; int pageIndex, startIndex, endIndex; //获取开始和结束的记录序号 PagerHelper.GetStartAndEndIndex(allCount, pageSize, out pageIndex, out startIndex, out endIndex); //调用存储过程返回指定序号范围的数据 // return View(db.SelectUserList(startIndex, endIndex)); //var sear = (from m in db.Movies where m.ID >= startIndex && m.ID <= endIndex select m).ToList(); var sear = db.Movies.OrderBy(m => m.ID).Skip(startIndex).Take(endIndex - startIndex + 1).ToList(); return View("Index", sear); //return View(db.Movies.ToList()); }
@model IEnumerable<MvcTest.Models.Movie> @using MvcTest.Extends @using MvcTest.HTML @{ ViewBag.Title = "Index"; } <script src="~/Scripts/jquery-1.7.1.js"></script> <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script> <h2>Index</h2> <script type="text/javascript"> function Call() { $.post('/Movies', { page: '2' }, OnPageChanged); } // function Hello() { // alert("hello"); // } </script> <p> @Html.ActionLink("Create New", "Create") </p> <div id="dvOrders"> <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.Title) </th> <th> @Html.DisplayNameFor(model => model.ReleaseDate) </th> <th> @Html.DisplayNameFor(model => model.Genre) </th> <th> @Html.DisplayNameFor(model => model.Price) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @* @Html.DisplayFor(modelItem => item.Title)*@ @item.ID </td> <td> @*@Html.DisplayFor(modelItem => item.ReleaseDate,"yyyy-MM-dd")*@ @Html.ValueFor(modelItem => item.ReleaseDate, "{0:yyyy-MM-dd}") </td> <td> @Html.DisplayFor(modelItem => item.Genre) </td> <td> @Html.DisplayFor(modelItem => item.Price) </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> } </table> @Html.Pager("GoPage", "Movies", new { }, new PagerConfig { TotalRecord = ViewBag.Num, PageSize = ViewBag.PageSize, UseAjax = true, AjaxUpdateTargetID = "dvOrders" }) </div> <!-- <input type="button" id="TestList" /> --> @section Scripts{ <script type="text/javascript"> function Format(date) { return date + "123"; } function formatNumToDate(value) { var now = eval(value.replace(//Date((d+))//gi, "new Date($1)"));///.../gi是用来标记正则开始和结束;是转义符;()标注了正则匹配分组1,$1 var year = now.getYear() + 1900; var month = now.getMonth() + 1; var date = now.getDate(); var hour = now.getHours(); var minute = now.getMinutes(); var second = now.getSeconds(); return year + "-" + compareNine(month) + "-" + compareNine(date) + " " + compareNine(hour) + ":" + compareNine(minute) + ":" + compareNine(second); } $(document).ready(function () { function ChangeDateFormat(time) { if (time != null) { var date = new Date(parseInt(time.replace("/Date(", "").replace(")/", ""), 10)); var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1; var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); return date.getFullYear() + "-" + month + "-" + currentDate; } return ""; } $("#TestList").click(function () { $.getJSON("/Movies/GetAllList", {}, function (result) { alert(result); $.each(result, function (i, field) { //var datetime = field.ReleaseDate.formatNumToDate(); alert(i + " " + ChangeDateFormat(field.ReleaseDate)); }); }); }); }); </script> }