Foreach来自viewbag的数据

时间:2015-09-28 11:44:05

标签: 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 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行上收到错误消息:

  

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

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

2 个答案:

答案 0 :(得分:6)

我可以看到你只是没有将你的收藏集传递给控制器​​中的CellViewModel查看。

你应该这样传递:

ObservableCollection<string> CellValue

然后在你的视图上:

ViewBag

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

如果要将此模型发布到控制器,则需要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> } 来解决绑定数组的MVC问题。

答案 1 :(得分:1)

正如teo van kot所指出的那样,你并没有将你的轮班分配给你的观察包..但它更好,更适合传递你的ShiftsModel作为模型,而不是通过ViewBag ...... 确保您的Index.cshtml文件具有以下using语句:

@model IEnumerable<ShiftsModel>

如果你像你已经做过那样传递你的模型:return View(shifts);那么你可以像这样迭代你的模型:

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