NopCommerce 增加 Customer Field

预期效果:

Customer表新增一个Column

该新增字段可以在Admin段 新增 修改 列表查询及显示

 

示例步骤:

0.数据库表修改

alter table [Customer] add MemberType nvarchar(2) ;

 

1.Entity处理

Libraries\Nop.Core\Domain\Customers\Customer.cs

仿照 Username 新增

/// <summary>

/// Gets or sets the Member Type

/// </summary>

public string MemberType { get; set; }

  

2.EntityMap处理

Libraries\Nop.Data\Mapping\Customers\CustomerMap.cs

新增

this.Property(u => u.MemberType).HasMaxLength(2);

 

3.EntityModel处理

Presentation\Nop.Web\Administration\Models\Customers\CustomerModel.cs

仿照 GenderEnabled 及 Gender 新增         

 

//可以不增加MemberTypeEnabled

//如需该设置的控制 可 NopCommerce 增加 Customer Settings

public bool MemberTypeEnabled { get; set; }

[NopResourceDisplayName("Admin.Customers.Customers.Fields.MemberType")]

[AllowHtml]

public string MemberType { get; set; }

 

 

4.增修页面

Presentation\Nop.Web\Administration\Views\Customer\_CreateOrUpdate.cshtml

 

仿照 @if (Model.CompanyEnabled) 新增

 

@if (Model.MemberTypeEnabled)

{

         <div class="form-group">

                   <div class="col-md-3">

                            @Html.NopLabelFor(model => model.MemberType)

                   </div>

                   <div class="col-md-9">

                            @Html.NopEditorFor(model => model.MemberType)

                            @Html.ValidationMessageFor(model => model.MemberType)

                   </div>

         </div>

}

                           

 

5.新增操作

Presentation\Nop.Web\Administration\Controllers\CustomerController.cs

 

进入页面时 Model准备

修改 protected virtual void PrepareCustomerModel 的Action

新增 model.MemberTypeEnabled = _customerSettings.MemberTypeEnabled;

 

 

新增操作保存时 Post处理

修改 public ActionResult Create(CustomerModel model, bool continueEditing, FormCollection form)

的  var customer = new Customer

新增 MemberType = model.MemberType,

 

 

6.修改操作

Presentation\Nop.Web\Administration\Controllers\CustomerController.cs

 

进入页面时 Model准备

修改 protected virtual void PrepareCustomerModel 的Action

if (customer != null) 分支下

新增  model.MemberType = customer.MemberType;

 

修改操作保存时 Post处理

修改 public ActionResult Edit(CustomerModel model, bool continueEditing, FormCollection form)

仿照 username 新增

//MemberType

if (_customerSettings.MemberTypeEnabled )

{                       

         customer.MemberType = model.MemberType;

}

 

 

7.列表页面 列表显示

 

Presentation\Nop.Web\Administration\Models\Customers\CustomerListModel.cs

仿照  ZipPostalCodeEnabled 新增

public bool MemberTypeEnabled { get; set; }

 

 

列表显示

Presentation\Nop.Web\Administration\Views\Customer\List.cshtml

         $("#customers-grid").kendoGrid

仿照 @if (Model.CompanyEnabled) 新增

@if (Model.MemberTypeEnabled)

         {

                            <text>{

                                     field: "MemberType",

                                     title: "@T("Admin.Customers.Customers.Fields.MemberType")",

         width: 200

},</text>

}

 

其 $("#customers-grid").kendoGrid 的 url: "@Html.Raw(Url.Action("CustomerList", "Customer"))",

Presentation\Nop.Web\Administration\Controllers\CustomerController.cs

对应的  protected virtual CustomerModel PrepareCustomerModelForList(Customer customer)

return new CustomerModel 增加 MemberType = customer.MemberType,

 

 

 

8.列表页面 新增查询条件

 

1).Model处理

Presentation\Nop.Web\Administration\Models\Customers\CustomerListModel.cs

仿照 SearchZipPostalCode  新增

[NopResourceDisplayName("Admin.Customers.Customers.List.SearchMemberType")]

[AllowHtml]

public string SearchMemberType { get; set; }

 

2).Service处理

Libraries\Nop.Services\Customers\ICustomerService.cs

IPagedList<Customer> GetAllCustomers

增加查询参数 string memberType=null,

 

Libraries\Nop.Services\Customers\CustomerService.cs

public virtual IPagedList<Customer> GetAllCustomers

增加查询参数 string memberType=null,

及仿照 username 增加其Linq过滤

if (!String.IsNullOrWhiteSpace(memberType))

    query = query.Where(c => c.MemberType.Contains(memberType));

 

3).Controller Action 处理

Presentation\Nop.Web\Administration\Controllers\CustomerController.cs

public ActionResult CustomerList(

var customers = _customerService.GetAllCustomers( 中 新增

 memberType:model.SearchMemberType,

        

4).页面处理

Presentation\Nop.Web\Administration\Views\Customer\List.cshtml

 

仿照 @if (Model.ZipPostalCodeEnabled) 新增

@if (Model.MemberTypeEnabled )

{

         <div class="form-group">

                   <div class="col-md-4">

                            @Html.NopLabelFor(model => model.SearchMemberType)

                   </div>

                   <div class="col-md-8">

                            @Html.NopEditorFor(model => model.SearchMemberType)

                   </div>

         </div>

}

 

js查找及新增SearchMemberType

 $("".concat("#@Html.FieldIdFor(model => model.SearchEmail),",

"#@Html.FieldIdFor(model => model.SearchUsername),",

"#@Html.FieldIdFor(model => model.SearchMemberType),",

 

 

js查找及新增SearchMemberType

function additionalData() {

var data = {

         SearchCustomerRoleIds: $('#@Html.FieldIdFor(model => model.SearchCustomerRoleIds)').val(),

         SearchEmail: $('#@Html.FieldIdFor(model => model.SearchEmail)').val(),

         SearchMemberType: $('#@Html.FieldIdFor(model => model.SearchMemberType)').val(),

                                                                                     

 

9.增加文本资源

 

运行站点

Admin -> Configuration -> Languages -> Edit

-> String resources -> Add new record

Admin.Customers.Customers.Fields.MemberType

Admin.Customers.Customers.List.SearchMemberType

        

        

10.解决方案 Clean 和 Rebuild

运行站点 查看效果

 

posted on 2016-12-14 14:43  freeliver54  阅读(809)  评论(0编辑  收藏  举报

导航