/// <summary> /// 根據條件,使用存儲過程分頁查詢電影 /// </summary> /// <param name="name"></param> /// <param name="time"></param> /// <param name="size"></param> /// ...
/// <summary>
/// 根據條件,使用存儲過程分頁查詢電影
/// </summary>
/// <param name="name"></param>
/// <param name="time"></param>
/// <param name="size"></param>
/// <param name="index"></param>
/// <returns></returns>
[WebMethod]
public string SearchMovince(string name,string time, int size, int index)
{
//1、定義存儲過程命令
string sql = "up_SearchMovie"; //2、定義sql參數對象
SqlParameter paraSize = new SqlParameter("@size", size);
SqlParameter paraIndex = new SqlParameter("@index", index);
SqlParameter paraName = new SqlParameter("@mName", name);
SqlParameter paraTime = new SqlParameter("@sTime", time); SqlParameter count = new SqlParameter("@totalCount", SqlDbType.Int);
count.Direction = ParameterDirection.Output; SqlParameter page = new SqlParameter("@totalPage", SqlDbType.Int);
page.Direction = ParameterDirection.Output; //3、定義數組,保存四個參數
SqlParameter[] paras = { paraSize, paraIndex, count, page, paraName, paraTime }; //4、調用DBHelper,執行存儲過程
DataTable dt = DBHelper.GetDataTable(sql, paras); //5、將返回的信息封裝到PageList對象
PageList list = new PageList();
list.TotalCount = Convert.ToInt32(count.Value);
list.TotalPage = Convert.ToInt32(page.Value);
list.MovieList = JsonConvert.DeserializeObject<List<Movie>>(JsonConvert.SerializeObject(dt)); return JsonConvert.SerializeObject(list);
} //Mvc調用 public ActionResult Search(int id, string txtName, string txtTime)
{
/*點擊上頁或者下頁,傳遞的是null*/
if (txtTime != null)
{
name = txtName;
time = txtTime;
} string json = webService.SearchMovince(name, time, 10, id);
PageList pageList = JsonConvert.DeserializeObject<PageList>(json); ViewBag.count = pageList.TotalCount;
ViewBag.page = pageList.TotalPage;
ViewBag.index = id;
List<Movie> list = pageList.MovieList.Where(s => (string.IsNullOrEmpty(txtName) ? true : s.MNane.Contains(txtName)) &&
(string.IsNullOrEmpty(txtTime) ? true : s.PlayTime == Convert.ToDateTime(txtTime))).ToList(); return PartialView("_PartialMovie", list.ToList());
} ///html 共 @ViewBag.count 條記錄,共 @ViewBag.page 頁,當前顯示第 @ViewBag.index 頁
@Ajax.ActionLink("首頁", "search", new { id = 1 }, new AjaxOptions { UpdateTargetId = "divMovie" })
@{
if (ViewBag.index == 1)
{
<span>上頁</span>
}
else
{
@Ajax.ActionLink("上頁", "search", new { id = ViewBag.index - 1 }, new AjaxOptions { UpdateTargetId = "divMovie" }) }
} @{
if (ViewBag.index == ViewBag.page)
{
<span>下頁</span>
}
else
{
@Ajax.ActionLink("下頁", "search", new { id = ViewBag.index + 1 }, new AjaxOptions { UpdateTargetId = "divMovie" })
}
}
@Ajax.ActionLink("尾頁", "search", new { id = ViewBag.page }, new AjaxOptions { UpdateTargetId = "divMovie" })
</p>