用viewbag中的数据Foreach

本文关键字:数据 Foreach viewbag | 更新日期: 2023-09-27 18:14:55

下面是我的代码。

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

我现在想在我的索引中引用它。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行说:

表示可由对象访问的强类型对象列表指数。

有什么建议我做错了吗?谢谢:)

用viewbag中的数据Foreach

正如我所看到的,您只是没有通过控制器中的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();
}

Then on your 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问题与数组绑定,如果您将此模型发布到控制器。

正如teo van kot指出的那样,您没有将您的班次分配给您的viewbag..但它是更好的,更合适的传递你的ShiftsModel作为模型,而不是通过ViewBag…确保你的索引。CSHTML文件有以下using语句:

@model IEnumerable<ShiftsModel>

如果你像之前那样传递你的模型:return View(shifts);,那么你可以像这样遍历你的模型:

@foreach(var shift in Model) 
{
   // do something with your shift
}