Razor Pages OnPost 方法中的 Page() 方法有什么作用?
What does Page() method in Razor Pages OnPost method do?
ASP 网络核心 2
我有以下页面模型代码
public class CreateModel : PageModel
{
private readonly IItemService _itemService;
public ItemCreateViewModel ItemModel { get; set; }
public CreateModel(IItemService itemService)
{
_itemService = itemService;
}
// all related data downloaded from db, so all rendered correctly
public async Task OnGet(ItemCreateViewModel itemModel)
{
ItemModel = await _itemService.GetCreateViewModel();
}
public async Task<IActionResult> OnPost(ItemCreateViewModel itemModel)
{
if (!ModelState.IsValid)
{
return Page();
}
var item = _itemService.CreateItem(itemModel);
var image = await ImagesController.ProcessFormImage(itemModel.ImageUpload.UploadPhoto, item.Id, ModelState);
if (!ModelState.IsValid)
{
//trouble is somewhere here
return Page();
}
await _itemService.SaveItemImage(image);
_itemService.CreateItem(itemModel);
return RedirectToPage("./Index");
}
}
当我尝试 post 错误的图像时,!ModelState.IsValid 生成 true 并调用 Page()。我得到
NullReferenceException: Object reference not set to an instance of an object.
in Model.ItemModel.Categories
其中 Categories
是几个 select 列表之一,通常由 _itemService.GetCreateViewModel() 从 db 下载。
我不明白 Page()
发生了什么。为什么不使用表单中的旧数据?如何重新呈现验证错误的页面(我在 cshtml 中有 asp-validation-for
)?我尝试了 this.ItemModel = itemModel
,但我没有再得到 Null 引用,但没有看到任何验证错误,之前的 select 列出了未保存的值。
return Page
与在正常 MVC 控制器操作中调用 return View()
相同。
When the submitted form has validation errors (that are passed to the server), the OnPostAsync
handler method calls the Page
helper method. Page
returns an instance of PageResult
. Returning Page
is similar to how actions in controllers return View
. PageResult
is the default return type for a handler method. A handler method that returns void
renders the page.
更新页面模型 属性 以允许视图绑定到模型。
public class CreateModel : PageModel {
private readonly IItemService _itemService;
public CreateModel(IItemService itemService) {
_itemService = itemService;
}
[BindProperty]
public ItemCreateViewModel ItemModel { get; set; }
// all related data downloaded from db, so all rendered correctly
public async Task<IActionResult> OnGetAsync() {
ItemModel = await _itemService.GetCreateViewModel();
return Page();
}
public async Task<IActionResult> OnPostAsync() {
if (!ModelState.IsValid) {
return Page();
}
var item = _itemService.CreateItem(ItemModel);
var image = await ImagesController.ProcessFormImage(ItemModel.ImageUpload.UploadPhoto, item.Id, ModelState);
if (!ModelState.IsValid) {
return Page();
}
await _itemService.SaveItemImage(image);
_itemService.CreateItem(ItemModel);
return RedirectToPage("./Index");
}
}
ASP 网络核心 2 我有以下页面模型代码
public class CreateModel : PageModel
{
private readonly IItemService _itemService;
public ItemCreateViewModel ItemModel { get; set; }
public CreateModel(IItemService itemService)
{
_itemService = itemService;
}
// all related data downloaded from db, so all rendered correctly
public async Task OnGet(ItemCreateViewModel itemModel)
{
ItemModel = await _itemService.GetCreateViewModel();
}
public async Task<IActionResult> OnPost(ItemCreateViewModel itemModel)
{
if (!ModelState.IsValid)
{
return Page();
}
var item = _itemService.CreateItem(itemModel);
var image = await ImagesController.ProcessFormImage(itemModel.ImageUpload.UploadPhoto, item.Id, ModelState);
if (!ModelState.IsValid)
{
//trouble is somewhere here
return Page();
}
await _itemService.SaveItemImage(image);
_itemService.CreateItem(itemModel);
return RedirectToPage("./Index");
}
}
当我尝试 post 错误的图像时,!ModelState.IsValid 生成 true 并调用 Page()。我得到
NullReferenceException: Object reference not set to an instance of an object.
in Model.ItemModel.Categories
其中 Categories
是几个 select 列表之一,通常由 _itemService.GetCreateViewModel() 从 db 下载。
我不明白 Page()
发生了什么。为什么不使用表单中的旧数据?如何重新呈现验证错误的页面(我在 cshtml 中有 asp-validation-for
)?我尝试了 this.ItemModel = itemModel
,但我没有再得到 Null 引用,但没有看到任何验证错误,之前的 select 列出了未保存的值。
return Page
与在正常 MVC 控制器操作中调用 return View()
相同。
When the submitted form has validation errors (that are passed to the server), the
OnPostAsync
handler method calls thePage
helper method.Page
returns an instance ofPageResult
. ReturningPage
is similar to how actions in controllers returnView
.PageResult
is the default return type for a handler method. A handler method that returnsvoid
renders the page.
更新页面模型 属性 以允许视图绑定到模型。
public class CreateModel : PageModel {
private readonly IItemService _itemService;
public CreateModel(IItemService itemService) {
_itemService = itemService;
}
[BindProperty]
public ItemCreateViewModel ItemModel { get; set; }
// all related data downloaded from db, so all rendered correctly
public async Task<IActionResult> OnGetAsync() {
ItemModel = await _itemService.GetCreateViewModel();
return Page();
}
public async Task<IActionResult> OnPostAsync() {
if (!ModelState.IsValid) {
return Page();
}
var item = _itemService.CreateItem(ItemModel);
var image = await ImagesController.ProcessFormImage(ItemModel.ImageUpload.UploadPhoto, item.Id, ModelState);
if (!ModelState.IsValid) {
return Page();
}
await _itemService.SaveItemImage(image);
_itemService.CreateItem(ItemModel);
return RedirectToPage("./Index");
}
}