从客户端调用对象后,出现错误代码CS1061

时间:2018-10-07 16:45:21

标签: c# asp.net-mvc asp.net-core razor-pages

  

严重性代码描述项目文件行抑制状态   错误CS1061“ LoginModel”不包含“模型”的定义   并且没有可访问的扩展方法“模型”接受第一个参数   可以找到“ LoginModel”类型(您是否缺少using指令   或组装   参考?)Lb_Clinics C:\ Users \ dibem \ source \ repos \ Clinics \ Lb_Clinics \ Pages \ Login.cshtml 25有效

这是尝试将我的object属性与文本字段绑定(以获得良好验证)时发生的错误。

我照常执行以下步骤:

  1. 使用对象的属性创建对象(例如:帐户)
  2. 创建从DbContext继承的模型类
  3. 将DbContext添加到服务
  4. 创建一个仅使用方法名称的接口
  5. 在存储库类中实现这些方法(例如:repo_Account)
  6. 创建网页剃刀而不是创建普通方法OnPost OnGet OnPut
  7. 使用方法(post,put)制作表单

我错过了什么吗?我试图理解此错误,我搜索了microsoft,它说在您调用不存在的方法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 _ DbContext:

   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-调用Interface使其与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]

1 个答案:

答案 0 :(得分: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]