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

时间:2015-07-24 12:32:26   收藏:0   阅读:4871

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

后台控制器: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

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!