【发布时间】:2020-09-25 11:13:35
【问题描述】:
我正在尝试从控制器显示表中的值以使用模型查看,但它显示错误。我已调试并检查值返回正常,但代码显示错误。我不知道问题出在哪里,请告诉我如何解决/修复此问题?
这是我的代码:
型号:
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");
}
}
房间状态视图:
@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”类型的模型项。基础模型'
【问题讨论】:
标签: asp.net-mvc asp.net-core entity-framework-core asp.net-core-3.0