C# 使用viewbag中的数据进行Foreach
下面是我的代码 型号C# 使用viewbag中的数据进行Foreach,c#,asp.net-mvc,razor,viewbag,C#,Asp.net Mvc,Razor,Viewbag,下面是我的代码 型号 public class ShiftsModel { public string UID { get; set; } public string Date { get; set; } public string Time { get; set; } public string Location { get; set; } } 控制器 public class HomeController : Controller { public
public class ShiftsModel
{
public string UID { get; set; }
public string Date { get; set; }
public string Time { get; set; }
public string Location { get; set; }
}
控制器
public class HomeController : Controller
{
public string xmlPath = HostingEnvironment.MapPath("~/App_Data/data.xml");
public ActionResult Index()
{
XDocument xml = XDocument.Load(xmlPath);
var shifts = (from b in xml.Descendants("Shift")
select new ShiftsModel
{
UID = (string)b.Attribute("UID"),
Date = (string)b.Element("Date"),
Time = (string)b.Element("Time"),
Location = (string)b.Element("Location")
}).ToList();
return View(shifts);
}
}
现在,我想在Index.cshtml文件中引用以下内容:
@foreach(var shift in (List<object>ViewBag.shifts)) {
<tr>
<td>
<input type="text" id="date" name="date" placeholder="Date" value="@(ViewBag.date)" }>
</td>
<td>
<input type="text" id="time" name="time" placeholder="Shift time" value="@(ViewBag.time)" }>
</td>
<td>
<input type="text" id="location" name="location" placeholder="Location" value="@(ViewBag.location)" }>
</td>
</tr>
}
@foreach(变量移入(ListViewBag.shifts)){
}
但是,我在ListViewBag.shifts
行中得到一个错误,它说:
表示可由访问的对象的强类型列表
索引
关于我做错了什么,有什么建议吗?谢谢:)正如我所见,您只是没有通过控制器中的
ViewBag
查看您的收藏
你应该这样通过:
public ActionResult Index()
{
XDocument xml = XDocument.Load(xmlPath);
var shifts = (from b in xml.Descendants("Shift")
select new ShiftsModel
{
UID = (string)b.Attribute("UID"),
Date = (string)b.Element("Date"),
Time = (string)b.Element("Time"),
Location = (string)b.Element("Location")
}).ToList();
ViewBag.shifts = shifts; // this line will pass your object
return View();
}
@foreach(var shift in Model)
{
// do something with your shift
}
那么在你看来,
@foreach(var shift in (List<ShiftsModel>ViewBag.shifts)) {
<tr>
<td>
<input type="text" id="date" name="date" placeholder="Date"
value="@(shift.Date)" }>
</td>
<td>
<input type="text" id="time" name="time" placeholder="Shift time"
value="@(shift.Time)" }>
</td>
<td>
<input type="text" id="location" name="location" placeholder="Location"
value="@(shift.Location)" }>
</td>
</tr>
}
视图:
@model List@*这是在视图中定义模型的位置*@
@对于(int i=0;iModel[i].Date,新的{placeholder=“Date”})
@TextBoxFor(x=>Model[i].Time,新{placeholder=“Shift-Time”})
@TextBoxFor(x=>Model[i].Location,新的{placeholder=“Location”})
}
如果要将此模型发布到控制器,则需要
for
forloop notforeach
来解决绑定阵列的MVC问题。正如teo van kot指出的,您并没有将班次分配给您的viewbag。。但它甚至更好,更适合作为模型传递您的ShiftsModel,而不是通过ViewBag。。。
确保Index.cshtml文件具有以下using语句:
@model IEnumerable<ShiftsModel>
根据您给出的代码,shift
是模型,而不是viewbag中的。您是否正在寻找@foreach(模型中的var移位)?您尚未向ViewBag
添加任何内容。将@model List
添加到视图中,并使用for(int i=0;im[i].Date….}
将(ListViewBag.shifts)
更改为(List)model.shifts
。如果您的视图没有任何模型,请添加一个适当的强类型模型(最好)或者将其声明为@model dynamic
(更差)或(更好)@model List
使用foreach访问(模型中的var移位)
@Nick请看最后一部分,您还需要更改foreach。@Nick不要担心!只需查看您已有的答案,它们显示了一个完整的工作示例!这将创建重复的id
属性(无效html)和重复的name属性(不带索引器)所以提交时不会绑定到任何模型。@StephenMuecke同意,但它将解决OP第一个问题,我更正我的错误answer@Nick您是否向shiftmodel类添加了funn名称空间?@Nick yeap,在您的视图中必须是@model List
@Nick visualstudio帮助您添加正确的名称空间只需编写列表
并检查它的建议
@model IEnumerable<ShiftsModel>
@foreach(var shift in Model)
{
// do something with your shift
}