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

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

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

梦想,是一个目标,是让自己活下去的原动力,是让自己开心的原因。坚持!
原文地址:https://www.cnblogs.com/tandy/p/4673047.html