C#MVC foreach循环视图中的模型和视图包

时间:2017-08-08 19:23:47

标签: c# asp.net-mvc view model

控制器:

public ActionResult SelectPlaats()
        {

            var one = repository.GetAllReserveringen.Where(x => x.StartDatum >= x.StartDatum
                    && x.StartDatum <= x.EindDatum).Select(p => p.Plaats);
            var two = repository.GetAllPlekken.Select(p => p.Plaatsnummer);
            ViewBag.vrijeplekken = two.Except(one).ToList();

            return View(repository.GetAllPlekken);
        }

查看:

@using Camping.Domain.Entities
@model IEnumerable<Plek>

@{
    ViewBag.Title = "Beschikbare campingplekken";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="panel panel-default">

    <div class="panel-heading">
        <h3>Overzicht van alle klanten</h3>
    </div>
    <div class="panel-body">
        <table class="table table-striped table-condensed table-bordered">
            <tr>
                <th>Plaatsnummer</th>
                <th>Veldnaam</th>
                <th>Type</th>
                <th>Vierkante meter</th>
                <th>Amp</th>
                <th>Wifi</th>
                <th>Water</th>
                <th>Riool</th>
                <th>CAI</th>
                <th>PPN</th>
                <th>Seizoenplek</th>
                <th class="text-center">Reserveren</th>
            </tr>

            @foreach (var item in Model.Where(x => x.Plaatsnummer == ViewBag.vrijeplekken))
            {
                <tr>
                    <td>@item.Plaatsnummer</td>
                    <td>@item.Veldnaam</td>
                    <td>@item.Type</td>
                    <td>@item.Vierkantemeter</td>
                    <td>@item.Amp</td>
                    <td>@item.Wifi</td>
                    <td>@item.Water</td>
                    <td>@item.Riool</td>
                    <td>@item.CAI</td>
                    <td>@item.PPN</td>
                    <td>@item.Seizoenplek</td>
                    <td class="text-center">
                        @using (Html.BeginForm("SelectPlek", "Reservering"))
                        {
                            @Html.Hidden("Id", item.Plaatsnummer)
                            <input type="submit"
                                   class="btn btn-default btn-xs"
                                   value="Reserveren" />
                        }
                    </td>
                </tr>
            }
        </table>
    </div>
</div>

所以我们的模型包含野营应用程序所有的campingsides +详细信息。但我只想在这个视图中显示免费的campingspots。

我从我的数据库中提取了所有免费的campingsides并将它们放在我的Viewbag.vrijeplekken中,其中包含sidenumbers(Plaatsnummer)列表。

我无法弄清楚如何只使用免费的campingsidenumbers获得一张桌子,但是所有campingsides都有详细信息。

我希望有人能帮助我。提前谢谢!

1 个答案:

答案 0 :(得分:2)

您的视图应该只关注它在(视图)模型中显示它接收的数据。 控制器可以选择要显示的数据。

正如德里克所说:

public Actionresult SelectPlaats()
{
   var one = repository.GetAllReserveringen.Where(x => x.StartDatum >= x.StartDatum && x.StartDatum <= x.EindDatum).Select(p => p.Plaats).ToList(); 
   return View(repository.GetAllPlekken.Where(p => !one.Contains(p.plaatsnummer)).ToList());
}

public Actionresult SelectPlaats()
{
   var one = repository.GetAllReserveringen.Where(x => x.StartDatum >= x.StartDatum && x.StartDatum <= x.EindDatum); 
   return View(repository.GetAllPlekken.Except(one).ToList());
}

在你的观点中:

@foreach (var item in Model)
 {
   ....