【问题标题】:Foreach with data from the viewbag使用 viewbag 中的数据进行 foreach
【发布时间】:2015-09-28 11:44:05
【问题描述】:

下面是我的代码。

型号

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&lt;object&gt;ViewBag.shifts 行收到错误消息:

表示一个强类型的对象列表,可以通过 索引。

请对我做错了什么有任何建议吗?谢谢你:)

【问题讨论】:

  • 根据您提供的代码,shifts 是模型,不在 viewbag 中。你在寻找@foreach(var shift in model) 吗?
  • 您尚未向ViewBag 添加任何内容。将@model List&lt;ShiftsModel&gt; 添加到视图并使用for(int i = 0; i &lt; Model.Count; i++) { @Html.TextBoxFor(m =&gt; m[i].Date ..... } 访问项目
  • 我猜,这只是一个错字,将(List&lt;object&gt;ViewBag.shifts) 更改为(List&lt;object&gt;)Model.shifts。如果您的视图没有任何模型,则添加适当的强类型模型(最好)或将其声明为@model dynamic(更差)或(更好)@model List&lt;ShiftModel&gt; 使用foreach (var shift in Model) 访问
  • @Nick 看最后一部分,你还需要改变你的 foreach。
  • @Nick 别担心!看看你已经得到的答案,它们显示了一个完整的工作示例!

标签: c# asp.net-mvc razor viewbag


【解决方案1】:

正如我所见,您只是不要通过控制器中的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();
}

然后在你的视图上:

    @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 问题。

【讨论】:

  • 这会创建重复的id 属性(无效的 html)和重复的名称属性(没有索引器),因此在提交时不会绑定到任何模型。
  • @StephenMuecke 同意,但它会解决 OP 第一个问题,我更正我的答案
  • @Nick 您是否将 funn 命名空间添加到您的 shiftmodel 类?
  • @Nick yeap,在你看来一定是@model List&lt;OvertimeAvailability.Models.ShiftsModel&gt;
  • @Nick Visual Studio 帮助您添加正确的命名空间,只需编写 List&lt;ShiftsModel&gt; 并查看它对您的建议
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 2012-09-10
    • 1970-01-01
    • 2015-03-14
    • 1970-01-01
    相关资源
    最近更新 更多