0

I'm doing an asp.net app and I have an error that I can't solve. I try to send a date from a view (index) to another view (selectServices) on a list. But after this action I get this error :

InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List'1[AppForSPA.Models.Service]', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.IEnumerable'1[AppForSPA.Models.Reservation]'.

So I supposed that is because I wrote @model IEnumerable<AppForSPA.Models.Service> instead of @model IEnumerable<AppForSPA.Models.Reservation>, in my selectServices view. I changed this but I still have the same error.

So here is my code, first my reservation controller with the index get and post methods :

// GET: Reservations/Index
public IActionResult Index() {
            return View();
        }

    // POST: Reservations/Index
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Index(CreateReservationViewModel model) {

        Reservation reservation;
        if (ModelState.IsValid) {
            reservation = new Reservation() { StartingDate = model.StartingDate };
            _context.Add(reservation);
            await _context.SaveChangesAsync();
            return RedirectToAction("SelectServices");
        }
        return View(model);
    }

Then my Reservation/Index view :

@model AppForSPA.Models.ReservationViewModels.CreateReservationViewModel

@{
    ViewData["Title"] = "Index";
}

<h2>New reservation</h2>


<form asp-action="SelectServices" >
    <div class="form-horizontal">
        <h4>Choose Date</h4>
        <hr />
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <div class="form-group">
            <label asp-for="StartingDate" class="col-md-2 control-label"></label>
            <div class="col-md-10">
                <input asp-for="StartingDate" type="date" class="form-control" />
                <span asp-validation-for="StartingDate" class="text-danger" />
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Select" class="btn btn-default" />
            </div>
        </div>
    </div>
</form>

<div>
    <a asp-action="Index">Back to List</a>
</div>
@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

So, from here i sent a date that the user need to choose, it must be listed in the next view "SelectServices". Here is the GET method of the action SelectServices of my reservationsController :

// GET: Reservations/SelectServices
        public async Task<IActionResult> SelectServices() {
            return View(await _context.Reservation.ToListAsync());

        }

Then the view selectServices :

@model IEnumerable<AppForSPA.Models.Reservation>
@{
    ViewData["Title"] = "SelectServices";
}

<h2>Select Services</h2>

<div asp-validation-summary="All" class="text-danger"></div>
<form asp-action="SelectServices" method="post">
    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.StartingDate)
                </th>
                <th>
                    Select Service
                </th>
            </tr>
        </thead>
        <tbody>
                @foreach (var item in Model) {
                    <tr>
                        <td>
                            @Html.DisplayFor(modelItem => item.StartingDate)
                        </td>

                        <td>
                            <input type="checkbox" name="IdsToAdd" value="@item.ReservationID">
                        </td>

                    </tr>
                }
            }
        </tbody>
    </table>

</form>

So maybe I forgot something ? I'm new in asp.net so I don't don't from where comes this error. If you need more code I can join it.

  • Your `_context.Reservation` is a DbSet for `Reservation` or `Service`? Also you cannot use a form on the selectServices page like this because your model is a list, not single object. – Roman Koliada Nov 30 '17 at 15:47
  • It's a DbSet for Reservation, first i write the date of the reservation in index page, then, I should be able to see the date that i wrote in a table in the selectService page. – Julien Haas Nov 30 '17 at 16:29

0 Answers0