Laden...

[gelöst] RazorPages 'IndexModel' does not contain a definition for

Erstellt von mchrd vor 5 Jahren Letzter Beitrag vor 5 Jahren 3.099 Views
M
mchrd Themenstarter:in
17 Beiträge seit 2018
vor 5 Jahren
[gelöst] RazorPages 'IndexModel' does not contain a definition for

Hallo zusammen,

ich bearbeite gerade ein Tutorial zum Thema RazorPages. Leider erhalte ich im Gegensatz zum Tutorial eine Fehlermeldung. Ich weiß wirklich nicht woran es liegen könnte und wäre über einen Tipp dankbar.

Die AccessModifier habe ich geprüft, aber daran sollte es nicht liegen.

Index.cshtml


@page
@model IndexModel
@{
}
<br />
<h2>Book List</h2>
<br />
<a asp-page="Create" class="btn btn-primary">Create New Book</a>

@if (Model.Books.Count() > 0)
{
    <form method="post">
<div>
    <br />
    <table class="table table-condensed table-hover">
        <tr><th>@Html.DisplayNameFor(m => m.Books.FirstOrDefault().ISBN)</th></tr>
        </table>"

</div>
</form>
}
else
{
    <p>No books found.</p>
}

Fehlermeldung:
_Severity Code Description Project File Line Suppression State
Error CS1061 'IndexModel' does not contain a definition for 'Books' and no accessible extension method 'Books' accepting a first argument of type 'IndexModel' could be found (are you missing a using directive or an assembly reference?) BookListRazor C:\BookListRazor\BookListRazor\Pages\BookList\Index.cshtml 10 Active

Severity Code Description Project File Line Suppression State
Error CS1061 'IndexModel' does not contain a definition for 'Books' and no accessible extension method 'Books' accepting a first argument of type 'IndexModel' could be found (are you missing a using directive or an assembly reference?) BookListRazor C:\BookListRazor\BookListRazor\Pages\BookList\Index.cshtml 16 Active_

Index.cshtml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;

namespace BookListRazor.Model.BookList
{
    public class IndexModel : PageModel
    {
        

        private ApplicationDbContext _db;

        public IndexModel(ApplicationDbContext db)
        {
            _db = db;
        }

        public IEnumerable<Book> Books { get; set; }
        public string SomeData { get; set; }

        public async void OnGet()
        {
            Books = await _db.Books.ToListAsync();
        }
    }
}

Folgendes ist installiert:
Microsoft.AspNetCore.App 2.1.1
Microsoft.AspNetCore.Razor.Design 2.1.2
Microsoft.EntityFrameworkCore.Sqlite 2.1.4
Microsoft.EntityFrameworkCore.Tools 2.1.4
Microsoft.NETCore.App 2.1.0

Über Tipps wäre ich dankbar!

Ich wünsche euch einen schönen Abend!

Der Folgende Abschnitt ist nicht mehr wichtig. Den Beitrag habe ich nochmal editiert, da ich einen Fehler schon lösen konnte. Wie weiß ich leider nicht, da immer noch die gleichen Pakete installiert sind. Das einzige was ich gemacht hatte, war das dotnet-sdk-2.2.103 runtergeladen und auf dem PC installiert. An den Installationen im Projekt habe ich nichts gemacht.

Ganz unabhängig von dieser Fehlermeldung habe ich leider noch folgende Fehlermeldungen:> Fehlermeldung:

_Severity Code Description Project File Line Suppression State
Error Package restore failed. Rolling back package changes for 'BookListRazor'.

Severity Code Description Project File Line Suppression State
Error NU1107 Version conflict detected for Microsoft.EntityFrameworkCore. Install/reference Microsoft.EntityFrameworkCore 2.2.1 directly to project BookListRazor to resolve this issue.
BookListRazor -> Microsoft.EntityFrameworkCore.Sqlite 2.2.1 -> Microsoft.EntityFrameworkCore.Sqlite.Core 2.2.1 -> Microsoft.EntityFrameworkCore.Relational 2.2.1 -> Microsoft.EntityFrameworkCore (≥ 2.2.1)
BookListRazor -> Microsoft.AspNetCore.App 2.1.1 -> Microsoft.EntityFrameworkCore (≥ 2.1.1 && < 2.2.0). BookListRazor C:\BookListRazor\BookListRazor\BookListRazor.csproj 1 _

Leider weiß ich nicht, wie ich das Problem lösen kann, jedoch muss ich zugeben, dass ich nicht mal wirklich die Fehlermeldung verstehe (nicht von der Sprache, sondern inhaltlich). Ich habe das Projekt mit Asp.net Core 2.1 erstellt (Target Framework ist auch 2.1).

M
mchrd Themenstarter:in
17 Beiträge seit 2018
vor 5 Jahren

Hab die Lösung gefunden: Einfach auf der ViewPage @using MyNamespace verwenden. Dann funktioniert es. Warum es im Tutorial so geht und bei mir nicht weiß ich dennoch nicht. Falls das jemand weiß, dann gerne her mit der Info 😃