【问题标题】:My View is not see View Model and Giving an error我的视图没有看到视图模型并给出错误
【发布时间】:2018-05-09 10:31:11
【问题描述】:

我的视图没有看到视图模型并给出表达式树可能不包含动态操作。当我寻找这个错误时,大多数人没有在视图上使用@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");
        }
    }

这是我的用户/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; }
    }
}

所以请帮助我理解它。

【问题讨论】:

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


    【解决方案1】:

    您对@Model 属性和@model 指令感到困惑,因为它们都是不同的东西,也有不同的用途。您需要输入@model 来定义视图模型。

    @model ArtSite.ViewModels.UserViewModel
    

    也请参阅this post (mvc uppercase Model vs lowercase model ) 了解更多详细信息。

    【讨论】:

      猜你喜欢
      • 2013-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-04
      • 1970-01-01
      • 2014-12-08
      • 1970-01-01
      • 2012-08-16
      相关资源
      最近更新 更多