·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> Orchard模块开发全接触6:自定义用户注册

Orchard模块开发全接触6:自定义用户注册

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

Orchard模块开发全接触6:自定义用户注册

我们都知道 Orchard 的用户注册相当简单,现在,我们需要一个自定义的用户注册,现在,开始吧。

一:定义实体

Models/CustomerPartRecord.cs:

public class CustomerPartRecord : ContentPartRecord{ public virtual string FirstName { get; set; } public virtual string LastName { get; set; } public virtual string Title { get; set; } public virtual DateTime CreatedUtc { get; set; }}

Models/CustomerPart.cs:

public class CustomerPart : ContentPart<CustomerPartRecord>{

public string FirstName { get { return Record.FirstName; } set { Record.FirstName = value; } }

public string LastName { get { return Record.LastName; } set { Record.LastName = value; } }

public string Title { get { return Record.Title; } set { Record.Title = value; } }

public DateTime CreatedUtc { get { return Record.CreatedUtc; } set { Record.CreatedUtc = value; } }}

Models/AddressPartRecord.cs:

public class AddressPartRecord : ContentPartRecord{ public virtual int CustomerId { get; set; } public virtual string Type { get; set; }}

Models/AddressPart.cs:

public class AddressPart : ContentPart<AddressPartRecord>{

public int CustomerId { get { return Record.CustomerId; } set { Record.CustomerId = value; } }

public string Type { get { return Record.Type; } set { Record.Type = value; } }}

修改Migrations.cs

public int UpdateFrom4(){ SchemaBuilder.CreateTable("CustomerPartRecord", table => table .ContentPartRecord() .Column<string>("FirstName", c => c.WithLength(50)) .Column<string>("LastName", c => c.WithLength(50)) .Column<string>("Title", c => c.WithLength(10)) .Column<DateTime>("CreatedUtc") );

SchemaBuilder.CreateTable("AddressPartRecord", table => table .ContentPartRecord() .Column<int>("CustomerId") .Column<string>("Type", c => c.WithLength(50)) );

ContentDefinitionManager.AlterPartDefinition("CustomerPart", part => part .Attachable(false) );

ContentDefinitionManager.AlterTypeDefinition("Customer", type => type .WithPart("CustomerPart") .WithPart("UserPart") );

ContentDefinitionManager.AlterPartDefinition("AddressPart", part => part .Attachable(false) .WithField("Name", f => f.OfType("TextField")) .WithField("AddressLine1", f => f.OfType("TextField")) .WithField("AddressLine2", f => f.OfType("TextField")) .WithField("Zipcode", f => f.OfType("TextField")) .WithField("City", f => f.OfType("TextField")) .WithField("Country", f => f.OfType("TextField")) );

ContentDefinitionManager.AlterTypeDefinition("Address", type => type .WithPart("CommonPart") .WithPart("AddressPart") );

return 5;}

Handlers/CustomerPartHandler.cs:

public class CustomerPartHandler : ContentHandler{ public CustomerPartHandler(IRepository<CustomerPartRecord> repository) { Filters.Add(StorageFilter.For(repository)); Filters.Add(new ActivatingFilter<UserPart>("Customer")); }}

注意哦,使用 UsrerPart,必须引用 Orchard.Users,于是乎,我们修改 Dependencies:

name: tminji.shopantiforgery: enabledauthor: tminji.comwebsite: http://www.tminji.comversion: 1.0.0orchardversion: 1.0.0description: The tminji.com module is a shopping module. Dependencies: Orchard.PRojections, Orchard.Forms, Orchard.jQuery, Orchard.jQuery, AIM.LinqJs, Orchard.Knockout, Orchard.Usersfeatures: shop: Description: shopping module. Category: ASample

注意哦,如果我们使用 UserPart,那么,我们就不能再 attach CommonPart,否则会导致 StackOverflowException

Handlers/AddressPartHandler.cs:

public class AddressPartHandler : ContentHandler{ public AddressPartHandler(IRepository<AddressPartRecord> repository) { Filters.Add(StorageFilter.For(repository)); }}

然后,Drivers/CustomerPartDriver.cs:

public class CustomerPartDriver : ContentPartDriver<CustomerPart>{

protected override string Prefix { get { return "Customer"; } }

protected override DriverResult Editor(CustomerPart part, dynamic shapeHelper) { return ContentShape("Parts_Customer_Edit", () => shapeHelper.EditorTemplate(TemplateName: "Parts/Customer", Model: part, Prefix: Prefix)); }

protected override DriverResult Editor(CustomerPart part, IUpdateModel updater, dynamic shapeHelper) { updater.TryUpdateModel(part, Prefix, null, null); return Editor(part, shapeHelper); }}

然后,Drivers/AddressPartDriver.cs:

public class AddressPartDriver : ContentPartDriver<AddressPart>{

protected override string Prefix { get { return "Address"; } }

protected override DriverResult Editor(AddressPart part, dynamic shapeHelper) { return ContentShape("Parts_Address_Edit", () => shapeHelper.EditorTemplate(TemplateName: "Parts/Address", Model: part, Prefix: Prefix)); }

protected override DriverResult Editor(AddressPart part, IUpdateModel updater, dynamic shapeHelper) { updater.TryUpdateModel(part, Prefix, null, null); return Editor(part, shapeHelper); }}

Views/EditorTemplates/Parts/Customer.cshtml:

@using System.Web.Mvc.Html@model TMinji.Shop.Models.CustomerPart<fieldset> <div class="editor-label">@Html.LabelFor(x => x.Title)</div> <div class="editor-field">@Html.EditorFor(x => x.Title)</div>

<div class="editor-label">@Html.LabelFor(x => x.FirstName)</div> <div class="editor-field"> @Html.EditorFor(x => x.FirstName) @Html.ValidationMessageFor(x => x.FirstName) </div>

<div class="editor-label">@Html.LabelFor(x => x.LastName)</div> <div class="editor-field"> @Html.EditorFor(x => x.LastName) @Html.ValidationMessageFor(x => x.LastName) </div></fieldset>

Views/EditorTemplates/Parts/Address.cshtml:

@using System.Web.Mvc.Html@model TMinji.Shop.Models.AddressPart<fieldset> <div class="editor-label">@Html.LabelFor(x => x.Type)</div> <div class="editor-field">@Html.EditorFor(x => x.Type)</div>

<div class="editor-label">@Html.LabelFor(x => x.CustomerId)</div> <div class="editor-field"> @Html.EditorFor(x => x.CustomerId) @Html.ValidationMessageFor(x => x.CustomerId) </div></fieldset>

Placement.info:

<Placement> <Place Parts_Product_Edit="Content:1" /> <Place Parts_Product="Content:0" /> <Place Parts_Product_AddButton="Content:after" /> <Place Parts_ShoppingCartWidget="Content:0" /> <Place Parts_Customer_Edit="Content:0" /> <Place Parts_Address_Edit="Content:0" /></Placement>

运行之,可以看到创建了数据库表:

image

二:前台准备

控制器 CheckoutController.cs:

using Orchard;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Web.Mvc;using Orchard.Mvc;using Orchard.Themes;

namespace TMinji.Shop.Controllers{ public class CheckoutController : Controller { private readonly IOrchardServices _services; private Localizer T { get; set; }

public CheckoutController(IOrchardServices services) { _services = services; }

[Themed] public ActionResult SignupOrLogin() {

return new ShapeResult(this, _services.New.Checkout_SignupOrLogin()); } }

}

创建视图 Checkout.SignupOrLogin.cshtml:

@using Orchard.ContentManagement@using Orchard.Core.Title.Models@using TMinji.Shop.Models