Asp.net mvc MVC查看并枚举列表
使用MVC3,我的控制器中有这个。我如何在视图中列举这些内容Asp.net mvc MVC查看并枚举列表,asp.net-mvc,asp.net-mvc-3,view,enumerable,Asp.net Mvc,Asp.net Mvc 3,View,Enumerable,使用MVC3,我的控制器中有这个。我如何在视图中列举这些内容 public ActionResult NWaySwitch() { var switches = _nWaySwitch.Switches.Cast<Switch>().ToList(); // var switches = _nWaySwitch.Switches; return View(switches); } public ActionRe
public ActionResult NWaySwitch()
{
var switches = _nWaySwitch.Switches.Cast<Switch>().ToList();
// var switches = _nWaySwitch.Switches;
return View(switches);
}
public ActionResult NWaySwitch()
{
var switches=_nWaySwitch.switches.Cast().ToList();
//变量开关=_nWaySwitch.switches;
返回视图(开关);
}
foreach语句无法对“object”类型的变量进行操作,因为“object”不包含“GetEnumerator”的公共定义
@foreach(ViewData[“开关”]中的System.Collections.ArrayList项)
{}我猜您在这里创建了一个强类型视图,对吗?如果查看视图的顶部,可能会看到模型定义。请确保您在IEnumerable中创建了此项,并且您应该可以很好地使用它我是这方面的新手,因此不,我没有使用强类型。良好的返回视图(开关)意味着您正在将模型传递给视图,因此,我们希望您的视图强类型化为Switch模型,在本例中更具体地说是IEnumerable模型,以便您可以访问视图中的数据。我想我知道了,但这将返回视图@foreach中Switch的命名空间错误(ViewData[“GetSwitchs”]中的var项作为IEnumerable)这意味着应用程序不知道在哪里可以找到交换机型号。尝试提供整个名称空间(即YourAppName.Models.Switch)。