// Copyright (c) .NET Foundation. All rights reserved. // 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.Http; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.Routing; using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.Extensions.DependencyInjection; namespace Microsoft.AspNetCore.Mvc.RazorPages { public abstract class PageModel { private PageArgumentBinder _binder; private IUrlHelper _urlHelper; /// /// Gets or sets the . /// public PageArgumentBinder Binder { get { if (_binder == null) { _binder = HttpContext?.RequestServices?.GetRequiredService(); } return _binder; } set { if (value == null) { throw new ArgumentNullException(nameof(value)); } _binder = value; } } /// /// Gets or sets the . /// public IUrlHelper Url { get { if (_urlHelper == null) { var factory = HttpContext?.RequestServices?.GetRequiredService(); _urlHelper = factory?.GetUrlHelper(PageContext); } return _urlHelper; } set { if (value == null) { throw new ArgumentNullException(nameof(value)); } _urlHelper = value; } } /// /// Gets the instance this model belongs to. /// public Page Page => PageContext?.Page; /// /// Gets the . /// [PageContext] public PageContext PageContext { get; set; } /// /// Gets the . /// public ViewContext ViewContext => PageContext; /// /// Gets the . /// public HttpContext HttpContext => PageContext?.HttpContext; /// /// Gets the . /// public HttpRequest Request => HttpContext?.Request; /// /// Gets the . /// public HttpResponse Response => HttpContext?.Response; /// /// Gets the . /// public ModelStateDictionary ModelState => PageContext.ModelState; /// /// Gets the from the . /// /// Returns null if is null. public ITempDataDictionary TempData => PageContext?.TempData; /// /// Gets the . /// public ViewDataDictionary ViewData => PageContext?.ViewData; /// /// Binds the model with the specified . /// /// The model type. /// The model name. /// A that on completion returns the bound model. protected internal Task BindAsync(string name) { return Binder.BindModelAsync(PageContext, name); } /// /// Binds the model with the specified . /// /// The model type. /// The model name. /// The default model value. /// A that on completion returns the bound model. protected internal Task BindAsync(TModel @default, string name) { return Binder.BindModelAsync(PageContext, @default, name); } /// /// Updates the specified instance using values from the controller's current /// . /// /// The type of the model object. /// The model instance to update. /// A that on completion returns true if the update is successful. protected internal Task TryUpdateModelAsync(TModel model) { return Binder.TryUpdateModelAsync(PageContext, model); } /// /// Updates the specified instance using values from the controller's current /// . /// /// The type of the model object. /// The model instance to update. /// The model name. /// A that on completion returns true if the update is successful. protected internal Task TryUpdateModelAsync(TModel model, string name) { return Binder.TryUpdateModelAsync(PageContext, model, name); } /// /// Creates a object that redirects () /// to the specified . /// /// The URL to redirect to. /// The created for the response. protected internal RedirectResult Redirect(string url) { return new RedirectResult(url); } /// /// Redirects () to the current page. /// /// The . protected RedirectToPageResult RedirectToPage() => RedirectToPage(pageName: null); /// /// Redirects () to the current page with the specified . /// /// The parameters for a route. /// The . protected RedirectToPageResult RedirectToPage(object routeValues) => RedirectToPage(pageName: null, routeValues: routeValues); /// /// Redirects () to the specified . /// /// The name of the page. /// The . protected internal RedirectToPageResult RedirectToPage(string pageName) => RedirectToPage(pageName, routeValues: null); /// /// Redirects () to the specified /// using the specified . /// /// The name of the page. /// The parameters for a route. /// The . protected internal RedirectToPageResult RedirectToPage(string pageName, object routeValues) => RedirectToPage(pageName, routeValues, fragment: null); /// /// Redirects () to the specified /// using the specified . /// /// The name of the page. /// The fragment to add to the URL. /// The . protected internal RedirectToPageResult RedirectToPage(string pageName, string fragment) => RedirectToPage(pageName, routeValues: null, fragment: fragment); /// /// Redirects () to the specified /// using the specified and . /// /// The name of the page. /// The parameters for a route. /// The fragment to add to the URL. /// The . protected internal RedirectToPageResult RedirectToPage(string pageName, object routeValues, string fragment) => new RedirectToPageResult(pageName, routeValues, fragment); /// /// Redirects () to the specified . /// /// The name of the page. /// The with set. protected internal RedirectToPageResult RedirectToPagePermanent(string pageName) => RedirectToPagePermanent(pageName, routeValues: null); /// /// Redirects () to the specified /// using the specified . /// /// The name of the page. /// The parameters for a route. /// The with set. protected internal RedirectToPageResult RedirectToPagePermanent(string pageName, object routeValues) => RedirectToPagePermanent(pageName, routeValues, fragment: null); /// /// Redirects () to the specified /// using the specified . /// /// The name of the page. /// The fragment to add to the URL. /// The with set. protected internal RedirectToPageResult RedirectToPagePermanent(string pageName, string fragment) => RedirectToPagePermanent(pageName, routeValues: null, fragment: fragment); /// /// Redirects () to the specified /// using the specified and . /// /// The name of the page. /// The parameters for a route. /// The fragment to add to the URL. /// The with set. protected internal RedirectToPageResult RedirectToPagePermanent(string pageName, object routeValues, string fragment) => new RedirectToPageResult(pageName, routeValues, permanent: true, fragment: fragment); /// /// Creates a object that renders the page. /// /// The . protected internal PageViewResult View() { return new PageViewResult(Page); } } }