Foreach 与来自 viewbag 的数据
Foreach with data from the 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 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>
}
但是,我在 List<object>ViewBag.shifts
行收到一条错误消息:
Represents a strongly typed list of objects that can be accessed by
index.
请问我做错了什么?谢谢:)
据我所知,您只是没有将 collection 传递给控制器中的 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 (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>
}
但是 MVC 解决问题的方法是像这样使用强类型视图:
控制器:
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();
ViewData.Model = shifts; // this line will pass your object but now to model
return View();
}
查看:
@model List<ShiftsModel> @*this is where your model is defined on view*@
@for(int i = 0; i < Model.Count(); i++) {
<tr>
<td>
@Html.TextBoxFor(x=> Model[i].Date, new { placeholder = "Date" })
</td>
<td>
@Html.TextBoxFor(x=> Model[i].Time, new { placeholder = "Shift time" })
</td>
<td>
@Html.TextBoxFor(x=> Model[i].Location, new { placeholder = "Location" })
</td>
</tr>
}
你需要 for
循环而不是 foreach
来解决绑定数组的 MVC 问题,如果你想 post 这个模型到控制器。
正如 teo van kot 指出的那样,您并没有将轮班分配给 viewbag。但将您的 ShiftsModel 作为模型而不是通过 ViewBag 传递会更好,也更合适...
确保您的 Index.cshtml 文件具有以下 using 语句:
@model IEnumerable<ShiftsModel>
如果您像之前那样传递模型:return View(shifts);
那么您可以像这样遍历模型:
@foreach(var shift in Model)
{
// do something with your shift
}
下面是我的代码。
型号
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>
}
但是,我在 List<object>ViewBag.shifts
行收到一条错误消息:
Represents a strongly typed list of objects that can be accessed by index.
请问我做错了什么?谢谢:)
据我所知,您只是没有将 collection 传递给控制器中的 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 (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>
}
但是 MVC 解决问题的方法是像这样使用强类型视图:
控制器:
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();
ViewData.Model = shifts; // this line will pass your object but now to model
return View();
}
查看:
@model List<ShiftsModel> @*this is where your model is defined on view*@
@for(int i = 0; i < Model.Count(); i++) {
<tr>
<td>
@Html.TextBoxFor(x=> Model[i].Date, new { placeholder = "Date" })
</td>
<td>
@Html.TextBoxFor(x=> Model[i].Time, new { placeholder = "Shift time" })
</td>
<td>
@Html.TextBoxFor(x=> Model[i].Location, new { placeholder = "Location" })
</td>
</tr>
}
你需要 for
循环而不是 foreach
来解决绑定数组的 MVC 问题,如果你想 post 这个模型到控制器。
正如 teo van kot 指出的那样,您并没有将轮班分配给 viewbag。但将您的 ShiftsModel 作为模型而不是通过 ViewBag 传递会更好,也更合适... 确保您的 Index.cshtml 文件具有以下 using 语句:
@model IEnumerable<ShiftsModel>
如果您像之前那样传递模型:return View(shifts);
那么您可以像这样遍历模型:
@foreach(var shift in Model)
{
// do something with your shift
}