【项目经验】之——Controller向View传值
2016-01-12 15:13
155 查看
我们的ITOO进行了一大部分了,整体上来说还是比较顺利的。昨天进行了一次验收,大体上来说,我们新生这块还是可以的。不仅仅进行了学术上的交流,还进行了需求上的更新。也正是由于这一次,我有了解到了一个新的需求,就是在我们界面转换之间的返回上,添加参数,使当前页上的数据还是跳转之前的样子。(前提不使用浏览器上的返回键,自己写一个返回按钮)
人 怕的不仅仅是不了解知识,更害怕的是没有想法!
了解到这个需求,我今天一上午都在想办法,试验了各种方法,结果用了一个最最简单的。下面就由我向大家分享一下:
就这样,我们先将当前页查询的信息存在Session中,然后再加载的时候用ViewBag(ViewBag
.DepName)来接收它,进行判断,剩下的只需要我们在前台中有一个对应的@ViewBag.DepName来接收从controller赋过来的值就可以了。然后,一切就都顺理成章了。
人 怕的不仅仅是不了解知识,更害怕的是没有想法!
了解到这个需求,我今天一上午都在想办法,试验了各种方法,结果用了一个最最简单的。下面就由我向大家分享一下:
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="width: 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传值的方法,我们应该了解它的原理。相关文章推荐
- iOS:个性化UITextView(缩进,行距,铺满)
- 快速排序
- 火狐浏览器扩展不兼容问题解决
- 深入学习Struts2
- C Primer Plus(第五版)4
- UDP通信 Java
- MongoDB介绍
- 解决读取iphone名称中文乱码问题
- Nexus入门指南(图文)
- mysql ddl 的相关操作
- Android安全研究经验谈
- lua_state
- Nginx域名跳转
- MyBatis框架
- IDEA / WebStorm / PhpStorm 添加jQuery自动提示,自动补全,提示文档
- Android安全研究经验谈
- 程序员之网络安全系列(五):数字证书以及12306的证书问题
- python数字图像处理(8):对比度与亮度调整
- 1.2 Quartz 2D 内存管理
- [转]jQuery 读取 xml