【项目经验】之——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="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传值的方法,我们应该了解它的原理。
【项目经验】之——Controller向View传值的更多相关文章
- 总结Controller向View传值的方式(转)
总结发现ASP.NET MVC中Controller向View传值的方式共有6种,分别是: ViewBag ViewData TempData 向普通View页面传一个Model对象 向强类型页面传传 ...
- ASP.NET MVC Controller向View传值方式总结
Controller向View传值方式总结 总结发现ASP.NET MVC中Controller向View传值的方式共有6种,分别是: ViewBag ViewData TempData 向普通Vie ...
- MVC:Controller向View传值方式总结
Controller向View传值方式总结 总结发现ASP.NET MVC中Controller向View传值的方式共有6种,分别是: ViewBag ViewData TempData 向普通Vie ...
- ASP.NET MVC Controller向View传值的几种方式
上几篇博文提到MVC和WebForm的区别,主要是MVC的Controller和View将传统的WebForm的窗体和后台代码做了解耦,这篇博文简单介绍一下在MVC中Controller向View是如 ...
- Controller向View传值方式总结
http://www.cnblogs.com/guohu/p/4377974.html 总结发现ASP.NET MVC中Controller向View传值的方式共有6种,分别是: ViewBag Vi ...
- MVC Controller 与 View 传值
Controller 到 View 1 强类型 控制器 // GET: /Test/ public ActionResult Index() { DateTime date = DateTime.No ...
- MVC Controller向View传值(ViewData与ViewBag)
近期在开发项目中,使用的是ASP.NET MVC因为之前并没有接触,对于它的传值方式有些陌生,在这里进行初步总结积累学习. 一:使用 ViewData使用: public ActionResult I ...
- MVC教程四:Controller向View传值的几种方式
一.通过ViewData传值 MVC从开始版本就一直支持使用ViewData将Controller里面的数据传递到View.ViewData定义如下: 从上面的截图中可以看出,ViewData里面存的 ...
- ASP.NET MVC 四种Controller向View传值方法
控制器: // Get: Data public ActionResult Index() { //ViewData 方式 ViewData["UserName"] = " ...
随机推荐
- c++ vector struct 使用
1. //test.h #include <string> using namespace std; struct AA { string a1; string a2; string a3 ...
- JAVA thread0.interrupt()方法
interrupt()只是改变中断状态而已,interrupt()不会中断一个正在运行的线程.这一方法实际上完成的是,给受阻塞的线程抛出一个中断信号,这样受阻线程就得以退出阻塞的状态. 更确切的说,如 ...
- 1.SpringMVC的简介和环境搭建
SpringMVC的简介: SpringMVC 和 Struts一样是一个MVC框架,和Spring无缝连接,和struts2类似, Spring MVC属于SpringFrameWork的后续产品, ...
- Lake Counting_深度搜索_递归
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30414 Accepted: 15195 D ...
- VB兼容问题
window7 64位无法显示打印窗问题 在Windows7 64位和VS2008环境下,PrintDialog.ShowDialog不能显示打印对话框 在VS2008中编写?如下代码: PrintD ...
- 【leetcode】Copy List with Random Pointer (hard)
A linked list is given such that each node contains an additional random pointer which could point t ...
- oracle一条sql语句统计充值表中今天,昨天,前天三天充值记录
select NVL(sum(case when create_date_time>=to_date('2014-11-24 00:00:00','yyyy-mm-dd hh24:mi:ss') ...
- IIS7.0配置网站时,提示“ISAPI 和 CGI 限制”
把网站配置到IIS上的时候,访问网站提示如下错误:
- 地图API文档
目录 腾讯地图API 2 1.API概览... 2 1.1 WebService API(官网注明是beta版本,可能不稳定,慎用):... 2 1.2 URL API:... 2 1.3 静态图AP ...
- hdu1014
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1014 //hdu1014 0ms #include<stdio.h> #include&l ...