doT.js的用法

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));
        }

       public static List<Dictionary<string, object>> dtTolist(DataTable dt)
        {
            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; ;
        }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值