【问题标题】:"InvalidOperationException" error showing values from model in ASP.NET Core MVC and Entity Framework Core在 ASP.NET Core MVC 和 Entity Framework Core 中显示模型值的“InvalidOperationException”错误
【发布时间】: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


    【解决方案1】:

    错误是抱怨视图模型的意外类型已传递给视图。

    期待FewoVerwaltung.Models.Base.BaseModel

    但是得到了List&lt;FewoVerwaltung.Models.RoomsStatus&gt;

    检查清单

    1. 模型类型

      我看到模型类型已经在视图中声明了

      @model IEnumerable<FewoVerwaltung.Models.RoomsStatus>
      

      但是错误表明它没有选择声明的模型类型,所以我会尝试重新编译项目,确保它运行的是最新的项目代码。

    2. 查看文件位置

      确保查看文件RoomsStatus.cshtml在文件夹~/Views/Rooms/

      ~/Views/Rooms/RoomsStatus.cshtml
      
    3. 控制器路由

      确保 URL,假设它是

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

      RoomsController控制器处理

    【讨论】:

    • 您好,感谢您的回复,我已经检查了您上面提到的所有内容,一切正常,当我从另一个控制器单击表行时,URL 看起来像这样,实际上我正在调用此控制器并在行上执行操作单击另一个控制器视图。单击row 后,URL 看起来像这样https://localhost:44334/Rooms/RoomsStatus?PartnerID=1&amp;BuldingID=11,但出现同样的错误,我也在使用baseControllerBaseModel,所以我需要在baseController 和@ 中进行哪些更改987654333@其实我是新手,正在对现有项目进行更改,老开发人员离开了这个项目,
    猜你喜欢
    • 2018-09-19
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 2017-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多