1

Controller:

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

View:

@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>

So we have the Model with all the campingsides + details my camping application has. But I only want to display in this view the free campingspots.

I extracted all the free campingsides from my database and put them in my Viewbag.vrijeplekken which holds a list of the sidenumbers (Plaatsnummer).

I can't figure out how to get a table with only the free campingsidenumbers but with the details all the campingsides have.

I hope anyone can help me out. Thanks in advance!

1
  • 2
    Why would you use the viewbag and not just add that as a part of your model and use it in your loop? Commented Aug 8, 2017 at 19:39

1 Answer 1

2

Your View should only concern itself with presenting the data it receives in the (View)model. It is up to the controller to select which data to display.

As Derek suggests:

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

or

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

And in your View:

@foreach (var item in Model)
 {
   ....
Sign up to request clarification or add additional context in comments.

3 Comments

I only do not really understand the code. How are the free campingsides connected with the general campingspots list?
in one you get a list of all the spots that are occupied. then the model you pass to the view contains a list of all the spots except the ones in 'one' (the occupied ones).
Ah I see now. I missed "!one". Thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.