[英]ASP.NET Core using MVC with Entity Framework - Edit method not showing values
[英]“InvalidOperationException” error showing values from model in ASP.NET Core MVC and Entity Framework Core
我正在尝试显示 controller 表中的值以使用 model 查看,但它显示错误。 我已调试并检查值返回正常,但代码显示错误。 我不知道问题出在哪里,请告诉我如何解决/修复此问题?
这是我的代码:
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; }
}
房间控制器:
//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: 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'
该错误是抱怨 View Model 的意外类型已传递给 View。
它期待FewoVerwaltung.Models.Base.BaseModel
,
但得到了List<FewoVerwaltung.Models.RoomsStatus>
我看到 model 类型已在视图中声明
@model IEnumerable<FewoVerwaltung.Models.RoomsStatus>
但是错误表明它没有选择声明的 model 类型,所以我会尝试重新编译项目,确保它运行的是最新的项目代码。
确保查看文件RoomsStatus.cshtml
位于文件夹~/Views/Rooms/
~/Views/Rooms/RoomsStatus.cshtml
确保 URL,假设它是
http://localhost:{int}/Rooms/RoomsStatus?PartnerID={int}&BuldingID={int}
由RoomsController
Controller 处理
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.