API 接口获取数据到cshtml界面

1.在model写好对象

 public class OrationViewModel
    {
        /// <summary>
        /// 自增长ID
        /// </summary>
        [JsonProperty("id")]
        public int Id { get; set; }
        /// <summary>
        /// 文章标题
        /// </summary>
        [JsonProperty("content_title")]
        public string Content_Title { get; set; }
    }

2.在controllers写好查询语句

    /// <summary>
        /// 获取新闻资讯
        /// </summary>
        /// <returns></returns>
        [HttpGet, ActionName("get")]
        public OrationPageViewModel GetOrationType(int pageSize, int pageIndex, int? type)
        {
            //获取资讯信息
            IPagedList<Contents> contents = _ioration_services.GetOrationWebType(pageSize, pageIndex, type);
            //赋值给分页控件
            OrationPageViewModel orationPageviewModel = new OrationPageViewModel();
            orationPageviewModel.PageIndex = contents.PageIndex;
            orationPageviewModel.PageSize = contents.PageSize;
            orationPageviewModel.TotalCount = contents.TotalCount;
            orationPageviewModel.TotalPages = contents.TotalPages;
            //将数据传给实体
            List<Contents> contentslist = contents as List<Contents>;

            contentslist.ForEach(item =>
            {
                OrationViewModel vm = new OrationViewModel();
                //编号
                vm.Id = item.Id;
                //标题
                vm.Content_Title = item.Content_Title;
                //内容
                vm.Content_Article = item.Content_Article;
                //文章时间
                vm.Content_times = item.Content_time != null ? ((long)item.Content_time).ConvertToIntDateTime().ToString("yyyy-M-d") : "";
                //文章类型
                vm.Content_Type_Id = item.Content_Type_Id;
                //文章类型名称
                vm.Content_Type_Name = item.Content_Type_Id == 1 ? "公司动态" : item.Content_Type_Id == 2 ? "业界新闻" : "家政常识";
                //创建人
                vm.Create_Name = item.Create_Name;
                orationPageviewModel.Oration.Add(vm);

            });
            return orationPageviewModel;
        }

 

3.写好JS获取数据到界面

//绑定新闻
var pageindex = 0;
var haspreviouspage = false;
var hasnextpage = true;
var totalpages = 0;

function GetNews(pageSize, pageIndex,type) {
 
    //清空界面
    $('.kuang').html("");
    var shenti = "";

    ajaxHelper('http://192.168.0.102/api/Oration/get?pageSize=' + pageSize + '&pageindex=' + pageIndex + '&type=' + type, 'GET', null).done(function (dt) {
        $.each(dt.oration, function (index, item) {
            shenti += '<tr>' +
                 '<th style="width: 59px;line-height:65px;font-weight:normal;text-align: center;font-size: 16px;color: #5b564c;display: none">' + item.id + '</th> ' +
                '<th style="width: 254px;line-height:65px;font-weight:normal;text-align: center;font-size: 16px;color: #5b564c;">' + '<a href="news-detauils.html?pageSize=' + 1 + '&pageindex=' + 0 + '&id=' + item.id + '">' + item.content_title + '</a>' + '</th> '+
                 '<th style="width: 150px;line-height:65px;font-weight:normal;text-align: center;font-size: 16px;color: #5b564c;white-space: nowrap;">' + item.create_name + '</th> ' +
                 '<th style="width: 196px;line-height:65px;font-weight:normal;text-align: center;font-size: 16px;color: #5b564c;white-space: nowrap;">' + item.content_times + '</th> ' +
                '</tr>'
        });
        //循环添加
        $('.kuang').append(shenti);
        //界面页数
        pageindex = dt.pageindex;
        //是否可以上一页
        haspreviouspage = dt.haspreviouspage;
        //是否可以下一页
        hasnextpage = dt.hasnextpage;
        //总页数
        totalpages = dt.totalpages;



        });
    }

 

1.注意:api必须发布之后才能在js里面获取到数据

自己摸索了很久,主要写好API之后,在url可一测试传值过去能不能查找到数据

 

posted @ 2015-12-28 16:36  如果冬天没有雪  阅读(872)  评论(1编辑  收藏  举报