您的位置:首页 > 其它

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

2015-07-24 11:52 246 查看
查了一上午资料,找到了一种比较有效的方法

后台控制器: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: