在上个月的 ASP CORE 2.0 新版本中,他们引入了 Razor 页面,这让我陷入了困境,因为 ASP CORE 2 Razor 页面中缺少旧控制器和模型 frpm MVC。
我从这个页面的理解是,我们使用[BindProperty] attribute
Action/Method 之外的 a 来获得属性的默认绑定!!!!,这是因为它移到了一个MVVM framework
相对于框架的MVC
框架。
- 问题:在尝试重写传统动作时,由于没有控制器,如何将代码移至新的 RazorPages MVVM 框架,即在何处以及如何绑定属性和动作/处理程序?
- 由于属性不在签名中,动作/处理程序如何知道哪些属性是从视图/剃刀页面传递给它的?
什么是页面模型?
public class CreateModel : PageModel // what is this pagemodel, is it new or the same old model?
{
private readonly AppDbContext _db;
public CreateModel(AppDbContext db)
{
_db = db;
}
[BindProperty]
public Customer Customer { get; set; } // why is the property outside?
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_db.Customers.Add(Customer);
await _db.SaveChangesAsync();
return RedirectToPage("/Index");
}
}