Skip to content

Commit

Permalink
UE pass on Partial views doc (#7417)
Browse files Browse the repository at this point in the history
Fixes #7322 

**Changes**
- Add Partial Tag Helper info (2.1+ only)
- Simplify & improve sample app and update to 2.1
- Add **Additional resources** section
- Apply Acrolinx suggestions
- Introduce headings in **Reference a partial view** section

[Internal Review Page](https://review.docs.microsoft.com/en-us/aspnet/core/mvc/views/partial?branch=pr-en-us-7417&view=aspnetcore-2.1)
  • Loading branch information
scottaddie committed Jul 3, 2018
1 parent ca3da66 commit 9121516
Show file tree
Hide file tree
Showing 80 changed files with 24,228 additions and 430 deletions.
165 changes: 116 additions & 49 deletions aspnetcore/mvc/views/partial.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
using Microsoft.AspNetCore.Mvc;
using System;
using PartialViewsSample.ViewModels;
using System;

namespace PartialViewsSample.Controllers
{
public class ArticlesController : Controller
{
public IActionResult Index()
{
return View();
}

public IActionResult Read()
{
var viewModel = GetArticle();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.AspNetCore.Mvc;

namespace PartialViewsSample.Controllers
{
public class HomeController : Controller
{
public IActionResult Discovery() => View();

public IActionResult Error() => View();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions aspnetcore/mvc/views/partial/sample/PartialViewsSample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace PartialViewsSample
{
public class Program
{
public static void Main(string[] args) =>
CreateWebHostBuilder(args).Build().Run();

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
Original file line number Diff line number Diff line change
@@ -1,55 +1,54 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace PartialViewsSample
{
public class Startup
{
public Startup(IHostingEnvironment env)
public Startup(IConfiguration configuration)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
Configuration = configuration;
}

public IConfigurationRoot Configuration { get; }
public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}

app.UseStaticFiles();
app.UseCookiePolicy();

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Articles}/{action=Index}/{id?}");
template: "{controller=Home}/{action=Discovery}/{id?}");
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace PartialViewsSample.ViewModels
public class Article
{
public string AuthorName { get; set; }

public DateTime PublicationDate { get; set; } = DateTime.Today;

public string Title { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!-- <snippet_ReadPartialView> -->
@model PartialViewsSample.ViewModels.Article

<h2>@Model.Title</h2>
@*Pass the author's name to Views\Shared\_AuthorPartial.cshtml*@
@await Html.PartialAsync("_AuthorPartial", Model.AuthorName)
@Model.PublicationDate

@*Loop over the Sections and pass in a section and additional ViewData
to the strongly typed Views\Articles\_ArticleSection.cshtml partial view.*@
@{
var index = 0;

@foreach (var section in Model.Sections)
{
<!-- <snippet_PartialAsync> -->
@await Html.PartialAsync("_ArticleSection", section,
new ViewDataDictionary(this.ViewData)
{
{ "index", index }
})
<!-- </snippet_PartialAsync> -->

index++;
}
}
<!-- </snippet_ReadPartialView> -->
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@using PartialViewsSample.ViewModels
@model ArticleSection

<h3>@Model.Title Index: @ViewData["index"] </h3>
<h3>@Model.Title Index: @ViewData["index"]</h3>
<div>
@Model.Content
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
@using PartialViewsSample.ViewModels
@{
ViewData["Title"] = "Partial view discovery";
ViewData["index"] = 0;
}

<table class="table table-hover">
<caption>Asynchronous partial view discovery examples</caption>
<thead>
<tr>
<th>Syntax</th>
<th>Result</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<code>
@Html.Raw(Html.Encode(@"<partial name=""_AuthorPartial"" />"))
</code>
<br />
<span class="badge">2.1+</span>
</td>
<td>
<!-- <snippet_PartialTagHelper> -->
<partial name="_AuthorPartial" />
<!-- </snippet_PartialTagHelper> -->
</td>
</tr>
<tr>
<td>
<code>
@Html.Raw(Html.Encode(@"<partial name=""_AuthorPartial.cshtml"" />"))
</code>
<br />
<span class="badge">2.1+</span>
</td>
<td>
<partial name="_AuthorPartial.cshtml" />
</td>
</tr>
<tr>
<td>
<code>
@Html.Raw(Html.Encode(@"<partial name=""~/Views/Home/_AuthorPartial.cshtml"" />"))
</code>
<br />
<span class="badge">2.1+</span>
</td>
<td>
<partial name="~/Views/Home/_AuthorPartial.cshtml" />
</td>
</tr>
<tr>
<td>
<code>
@Html.Raw(Html.Encode(@"<partial name=""/Views/Home/_AuthorPartial.cshtml"" />"))
</code>
<br />
<span class="badge">2.1+</span>
</td>
<td>
<partial name="/Views/Home/_AuthorPartial.cshtml" />
</td>
</tr>
<tr>
<td>
<code>
@Html.Raw(Html.Encode(@"<partial name=""../Articles/_ArticleSection.cshtml"" model='new ArticleSection { Title=""Section 1"", Content=""Coming soon"" }' view-data=""ViewData"" />"))
</code>
<br />
<span class="badge">2.1+</span>
</td>
<td>
<partial name="../Articles/_ArticleSection.cshtml"
model='new ArticleSection { Title="Section 1", Content="Coming soon" }'
view-data="ViewData" />
</td>
</tr>
<tr>
<td>
<code>
&#64;await Html.PartialAsync("_AuthorPartial")
</code>
</td>
<td>
<!-- <snippet_PartialAsync> -->
@await Html.PartialAsync("_AuthorPartial")
<!-- </snippet_PartialAsync> -->
</td>
</tr>
<tr>
<td>
<code>
&#64;{ await Html.RenderPartialAsync("_AuthorPartial"); }
</code>
</td>
<td>
<!-- <snippet_RenderPartialAsync> -->
@{
await Html.RenderPartialAsync("_AuthorPartial");
}
<!-- </snippet_RenderPartialAsync> -->
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="2">
<span class="badge">2.1+</span> Supported in ASP.NET Core 2.1 or later
</td>
</tr>
</tfoot>
</table>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<div>
<h3>Microsoft Docs</h3>
Some bio info here. Follow us at <a href="https://twitter.com/docsmsft">twitter.com/docsmsft</a>.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@model string
<div>
<h3>@Model</h3>
This partial view came from /Views/Shared/_AuthorPartial.cshtml.<br />
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
@using Microsoft.AspNetCore.Http.Features

@{
var consentFeature = Context.Features.Get<ITrackingConsentFeature>();
var showBanner = !consentFeature?.CanTrack ?? false;
var cookieString = consentFeature?.CreateConsentCookie();
}

@if (showBanner)
{
<nav id="cookieConsent" class="navbar navbar-default navbar-fixed-top" role="alert">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#cookieConsent .navbar-collapse">
<span class="sr-only">Toggle cookie consent banner</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<span class="navbar-brand"><span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span></span>
</div>
<div class="collapse navbar-collapse">
<p class="navbar-text">
Use this space to summarize your privacy and cookie use policy.
</p>
<div class="navbar-right">
<a asp-page="/Privacy" class="btn btn-info navbar-btn">Learn More</a>
<button type="button" class="btn btn-default navbar-btn" data-cookie-string="@cookieString">Accept</button>
</div>
</div>
</div>
</nav>
<script>
(function () {
document.querySelector("#cookieConsent button[data-cookie-string]").addEventListener("click", function (el) {
document.cookie = el.target.dataset.cookieString;
document.querySelector("#cookieConsent").classList.add("hidden");
}, false);
})();
</script>
}

0 comments on commit 9121516

Please sign in to comment.