Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit fb40862

Browse files
committed
Add a sample showing partial page updates in Razor Pages
1 parent 57d6bfe commit fb40862

File tree

9 files changed

+142
-0
lines changed

9 files changed

+142
-0
lines changed

Entropy.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mvc.ControllerForMultipleAr
129129
EndProject
130130
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mvc.CustomRazorPageHandlers", "samples\Mvc.CustomRazorPageHandlers\Mvc.CustomRazorPageHandlers.csproj", "{C673D327-412E-45E6-A12F-A8D37551793F}"
131131
EndProject
132+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mvc.RazorPagePartial", "samples\Mvc.RazorPagePartial\Mvc.RazorPagePartial.csproj", "{D98E6BE8-F1A8-47E7-BC34-017825DCF24E}"
133+
EndProject
132134
Global
133135
GlobalSection(SolutionConfigurationPlatforms) = preSolution
134136
Debug|Any CPU = Debug|Any CPU
@@ -715,6 +717,18 @@ Global
715717
{C673D327-412E-45E6-A12F-A8D37551793F}.Release|x64.Build.0 = Release|Any CPU
716718
{C673D327-412E-45E6-A12F-A8D37551793F}.Release|x86.ActiveCfg = Release|Any CPU
717719
{C673D327-412E-45E6-A12F-A8D37551793F}.Release|x86.Build.0 = Release|Any CPU
720+
{D98E6BE8-F1A8-47E7-BC34-017825DCF24E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
721+
{D98E6BE8-F1A8-47E7-BC34-017825DCF24E}.Debug|Any CPU.Build.0 = Debug|Any CPU
722+
{D98E6BE8-F1A8-47E7-BC34-017825DCF24E}.Debug|x64.ActiveCfg = Debug|Any CPU
723+
{D98E6BE8-F1A8-47E7-BC34-017825DCF24E}.Debug|x64.Build.0 = Debug|Any CPU
724+
{D98E6BE8-F1A8-47E7-BC34-017825DCF24E}.Debug|x86.ActiveCfg = Debug|Any CPU
725+
{D98E6BE8-F1A8-47E7-BC34-017825DCF24E}.Debug|x86.Build.0 = Debug|Any CPU
726+
{D98E6BE8-F1A8-47E7-BC34-017825DCF24E}.Release|Any CPU.ActiveCfg = Release|Any CPU
727+
{D98E6BE8-F1A8-47E7-BC34-017825DCF24E}.Release|Any CPU.Build.0 = Release|Any CPU
728+
{D98E6BE8-F1A8-47E7-BC34-017825DCF24E}.Release|x64.ActiveCfg = Release|Any CPU
729+
{D98E6BE8-F1A8-47E7-BC34-017825DCF24E}.Release|x64.Build.0 = Release|Any CPU
730+
{D98E6BE8-F1A8-47E7-BC34-017825DCF24E}.Release|x86.ActiveCfg = Release|Any CPU
731+
{D98E6BE8-F1A8-47E7-BC34-017825DCF24E}.Release|x86.Build.0 = Release|Any CPU
718732
EndGlobalSection
719733
GlobalSection(SolutionProperties) = preSolution
720734
HideSolutionNode = FALSE
@@ -779,6 +793,7 @@ Global
779793
{3D0B3DD1-6779-4E8F-BFBC-809497F7B30F} = {C03BF7E1-ECDF-48D0-85CD-A454AA28D80C}
780794
{8A61FA46-3FD4-42BE-80C6-E5931387590C} = {5BCA2B8A-37DA-4A88-A221-3D5B4796B07F}
781795
{C673D327-412E-45E6-A12F-A8D37551793F} = {8B204FB8-31A6-4559-AE7F-0412F504983C}
796+
{D98E6BE8-F1A8-47E7-BC34-017825DCF24E} = {5BCA2B8A-37DA-4A88-A221-3D5B4796B07F}
782797
EndGlobalSection
783798
GlobalSection(ExtensibilityGlobals) = postSolution
784799
SolutionGuid = {5DF584CA-249D-432C-BBA9-4498414C8E97}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<IsPackable>false</IsPackable>
5+
<TargetFrameworks>netcoreapp2.2;net461</TargetFrameworks>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="$(MicrosoftAspNetCoreMvcPackageVersion)" />
10+
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="$(MicrosoftAspNetCoreServerIISIntegrationPackageVersion)" />
11+
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="$(MicrosoftAspNetCoreServerKestrelPackageVersion)" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
@page "{handler?}"
2+
@model IndexModel
3+
4+
<div id="ajax-partial">
5+
<partial name="_IndexPartial" />
6+
</div>
7+
8+
<a href="#" data-ajax="true" data-ajax-update="#ajax-partial" data-ajax-url='@Url.Page(pageName: "")'>Update partial</a>
9+
10+
<form method="POST" data-ajax="true" data-ajax-update="#search-results" data-ajax-url='@Url.Page(pageName: "", pageHandler: "SearchBooks")'>
11+
<input type="submit" value="Search books" />
12+
13+
<div id="search-results"></div>
14+
</form>
15+
16+
<footer>Last refreshed at @DateTime.Now</footer>
17+
18+
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js"></script>
19+
<script src="https://ajax.aspnetcdn.com/ajax/mvc/3.0/jquery.unobtrusive-ajax.min.js"></script>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.AspNetCore.Mvc.RazorPages;
7+
using Microsoft.AspNetCore.Mvc.ViewFeatures;
8+
9+
namespace Mvc.RazorPagePartial.Pages
10+
{
11+
public class IndexModel : PageModel
12+
{
13+
public string Message => DateTime.Now.ToString();
14+
15+
public IActionResult OnGet() => Page();
16+
17+
public IActionResult OnGetPartial()
18+
{
19+
return new PartialViewResult
20+
{
21+
ViewName = "_IndexPartial",
22+
ViewData = ViewData,
23+
};
24+
}
25+
26+
public IActionResult OnPostSearchBooks()
27+
{
28+
var searchResults = new List<string>
29+
{
30+
$"Book 1 (ISBN: {Guid.NewGuid()})",
31+
$"Book 2 (ISBN: {Guid.NewGuid()})",
32+
};
33+
34+
return new PartialViewResult
35+
{
36+
ViewName = "_SearchResults",
37+
ViewData = new ViewDataDictionary<List<string>>(this.ViewData, searchResults),
38+
};
39+
}
40+
}
41+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@model IndexModel
2+
<h3>Partial last updated at @Model.Message</h3>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@model List<string>
2+
3+
<ul>
4+
@foreach (var book in Model)
5+
{
6+
<li>@book</li>
7+
}
8+
9+
</ul>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
2+
@namespace Mvc.RazorPagePartial.Pages
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Mvc.RazorPagePartial
2+
===
3+
4+
This application demonstrates using a model for performing partial updates using Razor Pages
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.IO;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.Mvc.ApplicationModels;
8+
using Microsoft.Extensions.DependencyInjection;
9+
10+
namespace Mvc.RazorPagePartial
11+
{
12+
public class Startup
13+
{
14+
public void ConfigureServices(IServiceCollection services)
15+
{
16+
services.AddMvc();
17+
}
18+
19+
public void Configure(IApplicationBuilder app)
20+
{
21+
app.UseMvc();
22+
}
23+
24+
public static void Main(string[] args)
25+
{
26+
var host = new WebHostBuilder()
27+
.UseContentRoot(Directory.GetCurrentDirectory())
28+
.UseIISIntegration()
29+
.UseKestrel()
30+
.UseStartup<Startup>()
31+
.Build();
32+
33+
host.Run();
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)