我正在尝试从控制器显示表中的值,以使用模型进行查看,但是显示错误。我已调试,并检查返回的值还可以,但是代码显示错误。我不知道问题出在哪里,请让我知道如何解决/解决此问题?
这是我的代码:
型号:
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视图:
@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>
这是我得到的错误:
Stack InvalidOperationException:传递到ViewDataDictionary中的模型项的类型为'System.Collections.Generic.List1 [FewoVerwaltung.Models.RoomsStatus]',但是此ViewDataDictionary实例需要类型为'FewoVerwaltung.Models.Base.BaseModel的模型项'
错误提示视图模型的意外类型已传递给视图。
期待FewoVerwaltung.Models.Base.BaseModel
,
但是得到了List<FewoVerwaltung.Models.RoomsStatus>
我看到模型类型已在视图中声明
@model IEnumerable<FewoVerwaltung.Models.RoomsStatus>
但是错误表明它没有使用声明的模型类型,因此我将尝试重新编译项目,确保它正在运行最新的项目代码。
确保查看文件RoomsStatus.cshtml
位于文件夹~/Views/Rooms/
中
~/Views/Rooms/RoomsStatus.cshtml
请确保网址,假设是
http://localhost:{int}/Rooms/RoomsStatus?PartnerID={int}&BuldingID={int}
由RoomsController
控制器处理