欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

存储过程 Mvc 的调用

程序员文章站 2022-06-21 14:01:52
/// /// 根据条件,使用存储过程分页查询电影 /// /// /// /// /// ......
/// <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>