【项目经验】之——Controller向View传值



       我们的ITOO进行了一大部分了,整体上来说还是比较顺利的。昨天进行了一次验收,大体上来说,我们新生这块还是可以的。不仅仅进行了学术上的交流,还进行了需求上的更新。也正是由于这一次,我有了解到了一个新的需求,就是在我们界面转换之间的返回上,添加参数,使当前页上的数据还是跳转之前的样子。(前提不使用浏览器上的返回键,自己写一个返回按钮)


       人 怕的不仅仅是不了解知识,更害怕的是没有想法!


       了解到这个需求,我今天一上午都在想办法,试验了各种方法,结果用了一个最最简单的。下面就由我向大家分享一下:


1、ViewBag:


controller代码:

<span style="font-size:18px;">#region DepReport()-获得后台数据-李卫中--2016年1月12日10:53:38
        /// <summary>
        /// DepReport()-获得后台数据
        /// </summary>
        /// <param name="strlike">学院名称</param>
        /// <returns></returns>
        public JsonResult DepReportList(string strlike)
        {

            int pageSize = Request["rows"] == null ? 10 : int.Parse(Request["rows"]);
            int pageIndex = Request["page"] == null ? 1 : int.Parse(Request["page"]);
            int total;
            <span style="color:#ff0000;"><strong>Session["DepName"] = strlike;</strong></span>

            List<FreshDepartmentViewModel> lsdep;

            if (strlike == "" || strlike == null || strlike == "请输入学院名称")
            {
                lsdep = depservice.FreshDepReport(pageSize, pageIndex, out total);
            }
            else
            {
                lsdep = depservice.FuzzyFreshDepReport(pageSize, pageIndex, out total, strlike);
            }


            var data = new
            {
                total,
                rows = lsdep
            };
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        #endregion</span>




<span style="font-size:18px;">public ActionResult Index()
        {
            if (Session["DepName"] != null)
            {
                <span style="color:#ff0000;"><strong>ViewBag.txtSearch = Session["DepName"];</strong></span>
            }
            else
            {
                ViewBag.txtSearch = "";
            }
            return View();
        }</span>


View展示:

<span style="font-size:18px;"><input id="txtSearch" <strong><span style="color:#ff0000;">value="@ViewBag.txtSearch"</span></strong> onkeyup="AutoSuggest(this, event, document.getElementById('urllink').value);" 
 onkeypress="if(keyCode==13) doSearch();" placeholder="请输入学院名称"  @*value="" class="gray" onclick="    if (this.value == '请输入学院名称') { this.value = ''; this.className = 'black' }" onblur="    if (this.value == '') { this.value = '请输入学院名称'; this.className = 'gray' }" *@style=" 320px; height: 20px; margin-bottom: 30px;" autocomplete="off" onkeydown="    if (event.keyCode == 13) { doSearch(); }" /></span>

        就这样,我们先将当前页查询的信息存在Session中,然后再加载的时候用ViewBag(ViewBag .DepName)来接收它,进行判断,剩下的只需要我们在前台中有一个对应的@ViewBag.DepName来接收从controller赋过来的值就可以了。然后,一切就都顺理成章了。




2、ViewData


只是其中的一种方法,另一种方法就是viewdata,相信大家用的比较广泛,类似于viewbag,我们要再前台用相应的@ViewData[ ]; 


Controller:

<span style="font-size:18px;">public ActionResult Index()
        {
            <span style="color:#ff0000;"><strong>ViewData["depname"] = "数学与信息科学学院";</strong></span>
            return View();
        }</span>


View

<span style="font-size:18px;"><input id="urllink" type="hidden" value="/FreshMajorReport/ProcessRequest?<strong><span style="color:#ff0000;">depname=@ViewData["depname"]</span></strong>" /></span>


总结:


          不怕不知道,就怕不知道。通过这次学习,更加了解了viewbag和viewdata的区别和作用,对于这两种用于controller向view传值的方法,我们应该了解它的原理。



原文地址:https://www.cnblogs.com/DoubleEggs/p/5747165.html