Skip to content

Commit

Permalink
Adds User extension example to Demo module (#1060)
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Payne authored and sebastienros committed Oct 3, 2017
1 parent b6d229f commit 6bfd95b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Threading.Tasks;
using OrchardCore.Demo.Models;
using OrchardCore.Demo.ViewModels;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.DisplayManagement.ModelBinding;
using OrchardCore.DisplayManagement.Views;
using OrchardCore.Entities.DisplayManagement;
using OrchardCore.Users.Models;

namespace OrchardCore.Demo.Drivers
{
public class UserProfileDisplayDriver : SectionDisplayDriver<User, UserProfile>
{
public override IDisplayResult Edit(UserProfile profile, BuildEditorContext context)
{
return Shape<EditUserProfileViewModel>("UserProfile_Edit", model =>
{
model.Age = profile.Age;
model.Name = profile.Name;
}).Location("Content:2");
}

public override async Task<IDisplayResult> UpdateAsync(UserProfile profile, IUpdateModel updater, BuildEditorContext context)
{
var model = new EditUserProfileViewModel();

if (await context.Updater.TryUpdateModelAsync(model, Prefix))
{
profile.Age = model.Age;
profile.Name = model.Name;
}

return Edit(profile);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OrchardCore.Demo.Models
{
public class UserProfile
{
public int Age { get; set; }
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Entities.DisplayManagement\OrchardCore.Entities.DisplayManagement.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Module.Targets\OrchardCore.Module.Targets.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Admin.Abstractions\OrchardCore.Admin.Abstractions.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.BackgroundTasks.Abstractions\OrchardCore.BackgroundTasks.Abstractions.csproj" />
Expand All @@ -15,11 +16,18 @@
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Environment.Navigation\OrchardCore.Environment.Navigation.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.ResourceManagement\OrchardCore.ResourceManagement.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.ResourceManagement.Abstractions\OrchardCore.ResourceManagement.Abstractions.csproj" />
<ProjectReference Include="..\..\OrchardCore\OrchardCore.Users.Core\OrchardCore.Users.Core.csproj" />
<ProjectReference Include="..\OrchardCore.Contents\OrchardCore.Contents.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(AspNetCoreVersion)" />
</ItemGroup>

<ItemGroup>
<None Update="Views\UserProfile.Edit.cshtml">
<PackagePath>assets\$(PackageId)\</PackagePath>
</None>
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions src/OrchardCore.Modules/OrchardCore.Demo/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
using OrchardCore.Data.Migration;
using OrchardCore.Demo.Commands;
using OrchardCore.Demo.ContentElementDisplays;
using OrchardCore.Demo.Drivers;
using OrchardCore.Demo.Services;
using OrchardCore.DisplayManagement.Descriptors;
using OrchardCore.DisplayManagement.Handlers;
using OrchardCore.Environment.Commands;
using OrchardCore.Environment.Navigation;
using OrchardCore.Modules;
using OrchardCore.Mvc.RazorPages;
using OrchardCore.Security.Permissions;
using OrchardCore.Users.Models;

namespace OrchardCore.Demo
{
Expand Down Expand Up @@ -60,6 +63,8 @@ public override void ConfigureServices(IServiceCollection services)
services.AddScoped<IDataMigration, Migrations>();
services.AddScoped<IPermissionProvider, Permissions>();

services.AddScoped<IDisplayDriver<User>, UserProfileDisplayDriver>();

services.Configure<RazorPagesOptions>(options =>
{
options.Conventions.AddModularPageRoute("/OrchardCore.Demo/Pages/Hello", "Hello");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OrchardCore.Demo.ViewModels
{
public class EditUserProfileViewModel
{
public int Age { get; set; }
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@model OrchardCore.Demo.ViewModels.EditUserProfileViewModel

<div class="form-group">
<label asp-for="Name">@T["Name"]</label>
<input asp-for="Name" type="text" class="form-control" placeholder="@T["Name"]" />
</div>

<div class="form-group">
<label asp-for="Age">@T["Age"]</label>
<input asp-for="Age" type="text" class="form-control" placeholder="@T["Age"]" />
</div>

0 comments on commit 6bfd95b

Please sign in to comment.