gpt4 book ai didi

c# - Foreach 与来自 viewbag 的数据

转载 作者:行者123 更新时间:2023-11-30 14:08:52 26 4
gpt4 key购买 nike

下面是我的代码。

型号

public class ShiftsModel
{
public string UID { get; set; }
public string Date { get; set; }
public string Time { get; set; }
public string Location { get; set; }
}

Controller

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.

请问我做错了什么?谢谢:)

最佳答案

如我所见,您只是不通过 Controller 中的 ViewBag 将您的集合传递给 View。

你应该像这样传递它:

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();
}

然后在你的 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 解决问题的方法是像这样使用强类型 View :

Controller :

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>
}

如果您要将此模型发布到 Controller ,则需要 for 循环而不是 foreach 来解决数组绑定(bind)的 MVC 问题。

关于c# - Foreach 与来自 viewbag 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32822185/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com