Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

"InvalidOperationException" error showing values from model in ASP.NET Core MVC and Entity Framework Core?

$
0
0
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'

Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>