从客户端调用对象后,我收到错误代码 CS1061

After call to an object from the client side I Got Error Code CS1061

Severity Code Description Project File Line Suppression State Error CS1061 'LoginModel' does not contain a definition for 'Model' and no accessible extension method 'Model' accepting a first argument of type 'LoginModel' could be found (are you missing a using directive or an assembly reference?) Lb_Clinics C:\Users\dibem\source\repos\Clinics\Lb_Clinics\Pages\Login.cshtml 25 Active

这是在尝试将我的对象 属性 与文本字段绑定时出现的错误(以获得良好的验证)。

我像往常一样进行以下步骤:

  1. 使用其属性创建对象(例如:帐户)
  2. 创建模型class继承自DbContext
  3. 将 DbContext 添加到服务中
  4. 制作一个仅采用方法名称的接口
  5. 在存储库中实施这些方法 class(示例:repo_Account)
  6. 创建 Page Razor 比创建普通方法 OnPost OnGet OnPut
  7. 用方法制作表格 (post , put )

我是不是错过了什么??我试图理解这个错误,我搜索了微软,它说当你调用一个不存在的方法时会发生这种情况 Error Code CS1061

这是我的实现: 1_Entity :

public class Account
    {
        public int AccountID { get; set; }

        [Required]
        [MinLength(3)]
        [MaxLength(100)]
        [DataType(DataType.EmailAddress)]
        public string UserName { get; set; }

        [Required]
        [MinLength(3)]
        [MaxLength(100)]
        [DataType(DataType.Password)]
        public string Password { get; set; }

        public bool Verified { get; set; }


    }

2 _ 数据库上下文:

   public class ClinicalDbContext:DbContext
    {
        public ClinicalDbContext(DbContextOptions options ):base(options)
        {
            Database.EnsureCreated();
        }
        public DbSet<Account> Accounts { get; set; }
    }

3- 服务配置

a-

 public Startup(IConfiguration configuration,IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder().
                SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json")
                .AddJsonFile("appsettings.development.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build() ;
        }

b-

services.AddDbContext<ClinicalDbContext>(option => {
                option.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            });

4- 指定方法的接口

     public interface IAccount
        {
            //AddNewAccount ChangePassword ChangeEmailAddress Verify_Login

            void AddNewAccount(Account account);
            void ChangePassword(int AccountID, string Password);
            void ChangeEmailAddress(int AccountID, string UserName);
            int Verify_login(string UserName, string Password);
        }

6 - 调用接口将其与 cshtml.cs (Login.cshtml.cs)

中的 IAction 事件绑定
  public class LoginModel : PageModel
    {

        private readonly IAccount _account;
        public LoginModel(IAccount account)
        {
            _account = account;
        }
        [BindProperty]
        public Account Account { get; set; }

        public IActionResult OnPostAsync(Account Account)
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }
            int ID = _account.Verify_login(Account.UserName, Account.Password);
            if (ID > 0)
            {
                return RedirectToPage("/Index");
            }
            return Page();
        }

5 - Repository that implement those methods
 public class Repo_Account :IAccount
    {
        #region Instance Of Account 
        private Account _Account;
        public Account Account { get { return new Account(); } set { _Account = value; } }
        #endregion

        #region private read only instance of database context
        private readonly ClinicalDbContext _db;
        #endregion
        #region Public constructor 

        public Repo_Account(ClinicalDbContext db)
        {
            _db = db;
        }
        #endregion
        //AddNewAccount ChangePassword ChangeEmailAddress Verify_Login
        #region Add new account
        public void AddNewAccount(Account account)
        {
            _db.Accounts.Add(account);
            _db.SaveChanges();
        }
        #endregion

最后是 cshtml 页面

    [![<input id="email" asp-for="Model.UserName" >
                                    <div class="invalid-feedback">
                                        <span asp-validation-for="Model.UserName" class="alert-danger"></span>
                                    </div>
 <input asp-for="Model.Password" class="form-control" >
                                <div class="invalid-feedback">
                                    <span asp-validation-for="Model.Password" class="alert-danger"></span>
                                </div>

<button type="submit" class="btn btn-primary btn-block" >
                                    Login
                                </button>][2]][2]

您尚未在登录页面中定义名为 Model 的 属性。您定义了一个名为 Account 的。更改代码如下:

[![<input id="email" asp-for="Account.UserName" >
 <div class="invalid-feedback">
     <span asp-validation-for="Account.UserName" class="alert-danger"></span>
 </div>
 <input asp-for="Account.Password" class="form-control" >
 <div class="invalid-feedback">
     <span asp-validation-for="Account.Password" class="alert-danger"></span>
 </div>

 <button type="submit" class="btn btn-primary btn-block" >Login</button>][2]][2]