首页 > 编程语言 > 详细

MVC前台获取ViewData的数组中的值

时间:2015-07-24 12:31:41      阅读:1746      评论:0      收藏:0      [点我收藏+]

查了一上午资料,找到了一种比较有效的方法

后台控制器:public ActionResult Index()  

{  

    List<string> colors = new List<string>();  

    colors.Add("red");  

    colors.Add("green");  

    colors.Add("blue");  

    ViewData["listColors"] = colors;

 return View();  

}  

前台界面:

 @foreach (var color in ViewData["listColors"] as List<string>)  

    {

 @color  

    }  

我认为这种比较清楚简单。

还有其他几种传值方式(View和Action之间的数据传输)

ViewBag动态型

后台控制器:public ActionResult Index()  

{  

    

  Dictionary<string, string> stackholder = new Dictionary<string, string>();
    stackholder.Add("Client", "Mr.  Client");
    stackholder.Add("Manager", "Mr. Joy");
    stackholder.Add("Team Leader", "Mr.Toy");
    stackholder.Add("Sr. developer", "Mr.dojoy");
    stackholder.Add("developer", "Mr. nodoy");
    ViewBag.stackholder = stackholder;

 return View();  

}  

前台界面:

 @ViewBag.stackholder

 

ViewData弱态型

Model动态类型

  后台:return View(data)//相当于存入ViewData.Model

    前台:Model

 

MVC前台获取ViewData的数组中的值

原文:http://www.cnblogs.com/tandy/p/4673047.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!