An unhandled exception occurred while processing the request. ASP.NET

162 views Asked by At

Changed: I created a simple project just to test whether everything is really that bad. I connected to the database and displayed directly on the Index page, and you will not believe everything worked, although I connected directly and displayed all the data using toListAsync(), but in View I did not change anything and tried the same in my project, but the problem remained

P.S. I hope someone can help me with the problem link Github

An error that occurs ⬇️
An unhandled exception occurred while processing the request. InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Linq.Enumerable+WhereListIterator`1[TicketsUA.Models.Tickets]', but this ViewDataDictionary instance requires a model item of type 'TicketsUA.Models.Tickets'.

Here this method does not work (

public async Task<IActionResult> FindTickets(Tickets tickets)
{
    var AllTickets = await _tickets.GetAll();
    var find = AllTickets.Where(ti => ti.StartAirport == tickets.StartAirport && ti.EndAirport == tickets.EndAirport);
    return View(find); 
}

This is where it should be transferred (Don't look at the fact that it's so scary, it's all for the test, nothing more)

@model IEnumerable<TicketsUA.Models.Tickets>


<section>
    <div class="background-color-tikets">
        <div class="container">
            <div class="info-panel">
                <div class="input-panel">
                    <input type="text" class="input-text bor-radius-left" placeholder="Звідки">
                    <input type="text" class="input-text" placeholder="Куди">
                    <input type="date" class="date-input">
                    <input type="date" class="date-input">
                    <select class="spad-menu bor-radius-right">
                        <option value="value1">Економ</option>
                        <option value="value1">Комфорт</option>
                        <option value="value2">Бізнес</option>
                        <option value="value3">Перший клас</option>
                    </select>
                    <button type="submit" class="find-tickets-button">Знайти квитки</button>
                </div>
            </div>
        </div>
    </div>
</section>

<section>
    @if (Model != null)
    {
        @foreach (var ticket in Model)
        {
            <div class="background-color-card">
                <div class="container">
                    <div class="card">
                        <div class="card-info">
                            <p class="card-airline">British Airways</p>
                            <p class="card-price">34 201₴</p>
                            <div class="cities">
                                <p class="departure-city">@ticket.StartAirport</p>
                                <p>----></p>
                                <p class="arrival-city">@ticket.EndAirport</p>
                            </div>
                            <div class="time-fly">
                                <p class="card-time-departure">@ticket.DepartureDate</p>
                                <p>----></p>
                                <p class="card-duration-arrival">@ticket.ArrivalDate</p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        }

    }
    else
    {
        <h1>Error! Is null!</h1>
    }

</section>


I've try different neural networks (Copilot, GPT, Claude) when I insert text with an error they all say my View wants to get an object and I pass it a list and when I ask them "How do I pass list" but they give me that code which is not a solution because I already have it)

1

There are 1 answers

0
Jason Pan On

The root cause is you add @model Tickets in the _Layout.cshtml Page.

enter image description here

Delete it and the issue was fixed.

enter image description here