这种交互方式运用MVC的设计模式,有一个简单的交互。
这是一种读取数据并分页。将所获取对象转换成Json格式
后台controller Ajax接口:
public ActionResult 视图名称 (int p=1,int s=1,string keyword="")
{
JsonpResult json = CommonBLL.GetErrorJSONP("初始化中");
try
{
var pm = new PageModel()
{
CurrentPage=p,
PageSize=s,
};
if (keyword != "")
{
pm.OrCondition.Add(new SearchCondition()
{
ConditionField="字段名称",
SearchType=SearchType.Like,
ConditionValue1=keyword,
});
}
var LstClass = ClassBLL.GetClass(pm);
var getval = new
{
success=true,
code=0,
data=LstClass,
datacount=pm.DataCount,
maxpage=pm.MaxPage,
};
json = new JsonpResult(getval);
}
catch(Exception ex)
{
json = CommonBLL.GetErrorJSONP(ex.Message);
}
return json;
}
Ajax:
$.ajax({
type: "POST",
url: gSiteURI + "FrontEnd/GetClassList",
data: "p=" + pm.CurrentPage +
"&s=" + pm.PageSize +
"&keyword=" + $("#keyword").val(),
dataType: "json",
success: function (jsonObj) {
if (jsonObj.success) {
pm.DataCount = jsonObj.datacount;
pm.MaxPage = jsonObj.maxpage;
pm.PageEventCallBack = function () { LoadData(); };
ModelPagerInit("Data_pager", "Data");
var sList = jsonObj.data;
var htmlStr = "";
for (var i = 0; i < sList.length; i++) {
var data = sList[i];
htmlStr += ‘
‘;htmlStr += ‘
‘;htmlStr += ‘
‘ + data.ClassName + ‘‘;htmlStr += ‘
‘;htmlStr += ‘‘;
htmlStr += ‘‘;
htmlStr += ‘‘;
htmlStr += ‘‘;
htmlStr += ‘‘;
htmlStr += ‘‘;
htmlStr += ‘
‘;htmlStr += ‘
‘;}
htmlStr = htmlStr !== "" ? htmlStr : "
暂无数据";$("#Data_tbody").html(htmlStr);
}
else {
alert(jsonObj.msg);
}
HideLoadingUI();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
HideLoadingUI();
}
});
}