用Viewbag中的数据进行Foreach

Nic*_*ick 3 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; }
}
Run Code Online (Sandbox Code Playgroud)

控制者

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

}
Run Code Online (Sandbox Code Playgroud)

我现在想像这样在我的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>
}
Run Code Online (Sandbox Code Playgroud)

但是,我在List<object>ViewBag.shifts网上看到一条错误消息:

表示索引可以访问的对象的强类型列表。

请问关于我在做什么错的任何建议?谢谢 :)

teo*_*kot 6

如我所见,您只是不将您的收藏集通过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();
}
Run Code Online (Sandbox Code Playgroud)

然后在您的视图上:

    @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>
    }
Run Code Online (Sandbox Code Playgroud)

但是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();
}
Run Code Online (Sandbox Code Playgroud)

视图:

@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>
}
Run Code Online (Sandbox Code Playgroud)

如果将此模型发布到控制器,则不需要for循环foreach来解决绑定数组的MVC问题。