我的视图未看到视图模型并出现错误

My View is not see View Model and Giving an error

我的视图没有看到视图模型,并且给出的表达式树可能不包含动态操作。当我查找此错误时,大多数人并未在视图中使用@model xxx。不过我用过

这是我的用户控制器页面

 public class UserController : Controller
    {
        private readonly DbManager _context;

        public UserController(DbManager context)
        {
            _context = context;
        }
        [HttpGet]
        public IActionResult Kayit()
        {
            UserViewModel vm = new UserViewModel();
            return View(vm);
        }

        [HttpPost]
        public async Task<IActionResult> KayitAsync([Bind("Isim,Soyad,DogumTarihi,KullaniciAdi,Sifre")]Kullanici k)
        {
            if (ModelState.IsValid)
            {
                _context.Kullanicilar.Add(k);
                await _context.SaveChangesAsync();
                return View("thanks");
            }
            return View("Index");
        }
    }

这是我的User/Kayit视图

@Model ArtSite.ViewModels.UserViewModel

<h2>Kayıt Sayfası</h2>

<div class="row">
    <div class="col-md-4">
        <form asp-controller="User" asp-action="Kayit" method="post">
            <div asp-validation-summary="All"></div>

            <div>
                <label asp-for="Isim"></label>
                <input asp-for="Isim" />
                <span asp-validation-for="Isim"></span>
            </div>
            <div>
                <label asp-for="Soyad"></label>
                <input asp-for="Soyad" />
                <span asp-validation-for="Soyad"></span>
            </div>
            <div>
                <label asp-for="DogumTarihi"></label>
                <input type="date" asp-for="DogumTarihi" />
                <span asp-validation-for="DogumTarihi"></span>
            </div>

            <div>
                <label asp-for="KullaniciAdi"></label>
                <input asp-for="KullaniciAdi" />
                <span asp-validation-for="KullaniciAdi"></span>
            </div>
            <div>
                <label asp-for="Sifre"></label>
                <input type="password" asp-for="Sifre" />
                <span asp-validation-for="Sifre"></span>
            </div>
            <div>
                <label asp-for="ConfirmSifre"></label>
                <input type="password" asp-for="ConfirmSifre" />
                <span asp-validation-for="ConfirmSifre"></span>
            </div>
            <div><input type="submit" value="Kayıt Ol" /></div>
        </form>
    </div>
</div>

这是我用于视图的视图模型

 namespace ArtSite.ViewModels
{
    public class UserViewModel
    {
        [Required]
        public string Isim { get; set; }
        [Required]
        public string Soyad { get; set; }
        [Required,DataType(DataType.Date),Display(Name = "Doğum Tarihi")]
        public DateTime DogumTarihi { get; set; }
        [Required,MinLength(6),MaxLength(30),Display(Name ="Kullanıcı Adı")]
        public string KullaniciAdi { get; set; }
        [Required,DataType(DataType.Password), MinLength(6), MaxLength(30)]
        [Display(Name ="Şifre")]
        public string Sifre { get; set; }
        [Required, DataType(DataType.Password), MinLength(6), MaxLength(30)]
        [Compare("Password",ErrorMessage ="Şifreniz Uyuşmadı."),Display(Name ="Şifreyi Onayla")]
        public string ConfirmSifre { get; set; }
    }
}

所以请帮我理解一下。

您混淆了 @Model 属性 和 @model 指令,因为它们是不同的东西并且有不同的目的。您需要输入 @model 来定义视图模型。

@model ArtSite.ViewModels.UserViewModel

也可以参考这个post(mvc uppercase Model vs lowercase model )更详细的了解这些。