我的视图没有看到视图模型并且给出表达式树可能不包含动态操作。当我查找此错误时,大多数人都没有在视图上使用@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 View
@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>
这是我用于View的视图模型
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; }
}
}
所以请帮助我理解它。
答案 0 :(得分:1)
你在@Model
属性和@model
指令之间混淆,因为它们都是不同的东西,也有不同的目的。您需要放置@model
来定义视图模型。
@model ArtSite.ViewModels.UserViewModel
另请参阅this post (mvc uppercase Model vs lowercase model )以了解更详细的信息。