简体   繁体   中英

“InvalidOperationException” error showing values from model in ASP.NET Core MVC and Entity Framework Core

I am trying to show values in table from controller to view using model but it shows an error. I have debugged and checked values are returned OK, but the code shows an error. I don't know where is problem please let me know how can I resolve/fix this issue?

Here is my code:

Model:

public class RoomsStatus
{
    [Key]

    public int Id { get; set; }
    public DateTime CheckInDateTime { get; set; }
    public DateTime CheckOutDateTime { get; set; }
    public decimal DailyPricePerBed { get; set; }
    public int AmountOfBeds { get; set; }
    public string PriceType { get; set; }
    public bool Paid { get; set; }
    public string Name { get; set; }

    public int RoomNumber { get; set; }
}

ApplicationDbConext:

public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    public DbSet<RoomsStatus> RSN { get; set; }
 }

RoomsController:

//view booking details and rooms status
public async Task<IActionResult> RoomsStatus(int PartnerID,int BuldingID)
{
        try
        {
            return this.View("RoomsStatus", await _context.RSN.FromSqlRaw("EXECUTE dbo.GetRoomsStatusByID {0},{1}", PartnerID, BuldingID).ToListAsync());
        }
        catch (Exception e)
        {
            //Logger.LogError(e, "Error while displaying booking.");
            return this.RedirectToAction("Error", "Base");
        }
}

RoomStatus view:

@model IEnumerable<FewoVerwaltung.Models.RoomsStatus>

<h1>RoomsStatus</h1>

 <table class="table">
  <thead>
    <tr>
        <th>
            Name
        </th>
     </tr>
</thead>
<tbody>
    @foreach (var item in Model)
    {
        <tr>
            <td>

                @Html.DisplayFor(modelItem => item.Name)
            </td>
        </tr>
    }
    </tbody>
 </table>

And this is the error I get:

Stack InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'System.Collections.Generic.List1[FewoVerwaltung.Models.RoomsStatus]', but this ViewDataDictionary instance requires a model item of type 'FewoVerwaltung.Models.Base.BaseModel'

The error is complaining unexpected type of View Model has been passed to the View.

It's expecting FewoVerwaltung.Models.Base.BaseModel ,

but got List<FewoVerwaltung.Models.RoomsStatus>

Check List

  1. Model Type

    I see model type has been declared in the view

    @model IEnumerable<FewoVerwaltung.Models.RoomsStatus>

    But the error shows that it's not picking up the model type declared, so I would try recompiling the project, make sure it's running the latest project code.

  2. View file location

    Make sure the View file RoomsStatus.cshtml is in folder ~/Views/Rooms/

     ~/Views/Rooms/RoomsStatus.cshtml
  3. Controller route

    Make sure the URL, assuming it's

    http://localhost:{int}/Rooms/RoomsStatus?PartnerID={int}&BuldingID={int}

    is handled by the RoomsController Controller

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM