mvc项目中要用到ajax。从网上找了一套模版。用法简介如下。
1,引用js
<script src="https://github.com/olado/doT/raw/master/doT.js"></script>
2,html代码
<ul class="main_huod" id="flvideooo">
{{~it.rows :v:index}}
<li><a href="javascript:void(0);" οnclick="" class="fl">
{{=v.VideoName}} </a><span class="fr"> {{=v.CreateDate}} </span><div
class="clear">
</div>
</li>
{{~}}
</ul>
3,jquery,ajax调用并替换模版
var source = document.getElementById("flvideooo").innerHTML;
var template1 = doT.template(source);
function getfvideodata(sid) {
jQuery.ajax({
dataType: "json",
url: '/Home/BindFVideoByStoreid',//home是控制器,BindFVideoByStoreid'是控制器中的方法
data: { storeid: sid },//传递过去的参数
fail: function () {
alert("failed");
},
success: function (data) {
jQuery("#flvideooo").html(template1({ rows: data }));
}
});
}
4,控制器中的方法
public JsonResult BindFVideoByStoreid(string storeid)
{
DataTable dt1 = new DataTable();
if (string.IsNullOrEmpty(storeid))
{
storeid = model.FistFstoreid;
}
dt1 = model.GetFvideoBystoreid(storeid);//model为模型层对象
return Json(dtTolist(dt1));
}
{
List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
foreach (DataRow dr in dt.Rows)
{
Dictionary<string, object> result = new Dictionary<string, object>();
foreach (DataColumn dc in dt.Columns)
{
result.Add(dc.ColumnName, dr[dc].ToString());
}
list.Add(result);
}
return list; ;
}