From 9264f3aa2d6478f76adcb5743c5ba6bb9ea2bec6 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Tue, 7 Feb 2017 21:16:40 -0800 Subject: [PATCH] Add TryUpdateModelAsync to pages --- .../Infrastructure/PageArgumentBinder.cs | 29 +++++++++++++++++-- .../PageModel.cs | 21 ++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageArgumentBinder.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageArgumentBinder.cs index 4b76587999..4b40ac16cc 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageArgumentBinder.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/Infrastructure/PageArgumentBinder.cs @@ -9,10 +9,33 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure { public abstract class PageArgumentBinder { - public async Task BindModelAsync(PageContext context, Type type, object defaultValue, string name) + public async Task BindModelAsync(PageContext context, Type type, object @default, string name) { - var result = await BindAsync(context, value: null, name: name, type: type); - return result.IsModelSet ? result.Model : defaultValue; + var result = await BindAsync(context, null, name, type); + return result.IsModelSet ? result.Model : @default; + } + + public Task BindModelAsync(PageContext context, string name) + { + return BindModelAsync(context, default(T), name); + } + + public async Task BindModelAsync(PageContext context, T @default, string name) + { + var result = await BindAsync(context, null, name, typeof(T)); + return result.IsModelSet ? (T)result.Model : @default; + } + + public async Task TryUpdateModelAsync(PageContext context, T value) + { + var result = await BindAsync(context, value, string.Empty, typeof(T)); + return result.IsModelSet && context.ModelState.IsValid; + } + + public async Task TryUpdateModelAsync(PageContext context, T value, string name) + { + var result = await BindAsync(context, value, name, typeof(T)); + return result.IsModelSet && context.ModelState.IsValid; } protected abstract Task BindAsync(PageContext context, object value, string name, Type type); diff --git a/src/Microsoft.AspNetCore.Mvc.RazorPages/PageModel.cs b/src/Microsoft.AspNetCore.Mvc.RazorPages/PageModel.cs index 5688c20d9a..46f1d0e3c5 100644 --- a/src/Microsoft.AspNetCore.Mvc.RazorPages/PageModel.cs +++ b/src/Microsoft.AspNetCore.Mvc.RazorPages/PageModel.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure; using Microsoft.AspNetCore.Mvc.ViewFeatures; @@ -45,6 +46,26 @@ namespace Microsoft.AspNetCore.Mvc.RazorPages public ViewDataDictionary ViewData => PageContext?.ViewData; + protected Task BindAsync(string name) + { + return Binder.BindModelAsync(PageContext, name); + } + + protected Task BindAsync(T @default, string name) + { + return Binder.BindModelAsync(PageContext, @default, name); + } + + protected Task TryUpdateModelAsync(T value) + { + return Binder.TryUpdateModelAsync(PageContext, value); + } + + protected Task TryUpdateModelAsync(T value, string name) + { + return Binder.TryUpdateModelAsync(PageContext, value, name); + } + protected IActionResult Redirect(string url) { return new RedirectResult(url);