NOPCommerce 增加功能 颜色和尺码管理研究实现:

添加颜色和尺码管理菜单(配图是网友使用的Nop1.9修改的效果,本文是2.4 MVC架构下的):


                             

打开Administration下面的Sitemap:

服装颜色:<siteMapNodetitle="Color attributes" nopResource="Admin.Catalog.Attributes.ColorAttributes"controller="ColorAttribute" action="List"/>

服装尺码:<siteMapNodetitle="Size attributes" nopResource="Admin.Catalog.Attributes.SizeAttributes"controller="SizeAttribute" action="List"/>

同步需要进行的事情:

1. 增加ResourceString: Admin.Catalog.Attributes.ColorAttributes=Color/”颜色”

2.  Admin.Catalog.Attributes.SizeAttributes=Size/”尺码”

   insert into LocaleStringResource

values(1,'Admin.Catalog.Attributes.ColorAttributes','Color Attribute')

insert into LocaleStringResource

values(1,'Admin.Catalog.Attributes.SizeAttributes','Size Attribute')

3. 增加Controller:

具体实现可以参考CheckoutAttributeController.cs

4.       增加Action:public ActionResultList()

        { returnView();        }

增加对应的Partial View

请注意以上只是架构并未实现实际代码,View也没有内容现实

 后台的Mapping代码: Infrastructure\AutoMapperStartupTask.cs||MappingExtensions.cs

using Nop.Admin.Models.Snatches;

using Nop.Core.Domain.Snatches;

通过比对Model和Entity之间的差异:

//Snatch

            Mapper.CreateMap<Snatch,SnatchModel>()

               .ForMember(dest => dest.FriendlyName, mo => mo.MapFrom(src =>src.ProductVariant.Name));

   Mapper.CreateMap<SnatchModel,Snatch>()

                .ForMember(dest => dest.ProductVariantID, mo => mo.Ignore());

//以上方法会在IstartupTask中被调用到:

public static SnatchModel ToModel(thisSnatch entity)

        {

            return Mapper.Map<Snatch, SnatchModel>(entity);

        }

 

        public static Snatch ToEntity(thisSnatchModel model)

        {

            return Mapper.Map<SnatchModel, Snatch>(model);

        }

 

        public static Snatch ToEntity(thisSnatchModel model, Snatchdestination)

        {

            return Mapper.Map(model,destination);

        }

颜色管理
 

接下来要填充点内容了,否则菜单点下去看到空白的View. MVC中先建立Model的类ColorAttributeModel:

namespace Nop.Admin.Models.Catalog    public class ColorAttributeModel//

public class BaseNopEntityModel : BaseNopModel

    {

        public virtual int Id { get; set; }

    }

[NopResourceDisplayName("Admin.Catalog.Attributes.ColorAttributes.Fields.Name")]

        [AllowHtml]

        public string Name { get; set; }        [NopResourceDisplayName("Admin.Catalog.Attributes.ColorAttributes.Fields.ColorImageUrl")]

        public stringColorImageUrl { get; set;}


 

 

ColorAttributeController,其中第一个需要完成可以测试的代码,Binding-List Action

  

public ActionResultList()

        {

            IList<ColorAttributeModel>activityColorAttributeModel=new List<ColorAttributeModel>();

           activityColorAttributeModel.Add(

               new ColorAttributeModel(){ Name = "red", ColorImageUrl = "~/content/images/Green.png" } 

               );

           activityColorAttributeModel.Add(

            new ColorAttributeModel(){ Name = "black", ColorImageUrl = "~/content/images/Black.png" }

            );

            var gridModel = new GridModel<ColorAttributeModel>

            {

               Data = activityColorAttributeModel,

               Total = activityColorAttributeModel.Count()

            };

            return View(gridModel);

           

        }


 

 

View的主要代码,布局类似上截图:

<table class="adminContent">

    <tr>

        <td>

            @(Html.Telerik().Grid<ColorAttributeModel>(Model.Data)

                   .Name("colorattributes-grid")

                   .Columns(columns=>

                   {

                        columns.Bound(x =>x.Name)

                            .Width(300);

                        columns.Bound(x =>x.ColorImageUrl)

                                .Template(

                                    @<text>

                                        <img alt="@item.Id"src="@item.ColorImageUrl"/>

                                    </text>

                                ) //通过Template和Link设置链接

                                .ClientTemplate("<imgalt='<#= Id #>' src='<#= ColorImageUrl #>' />");

                        columns.Bound(x =>x.Id)

                            .Width(50)

                            .Centered()

                            .Template(x =>Html.ActionLink(T("Admin.Common.Edit").Text,"Edit", new{ id = x.Id }))

                            .ClientTemplate("<a href=\"Edit/<#= Id#>\">" + T("Admin.Common.Edit").Text+ "</a>")

                            .Title(T("Admin.Common.Edit").Text);

                   })

//通过List Action绑定数据源

                   .DataBinding(dataBinding => dataBinding.Ajax().Select("List", "ColorAttribute"))

                   .EnableCustomBinding(true))

        </td>

    </tr>

</table>


 

The URL is incorrectJ//所以如何获取正确的URL需要学习下?很简单,之前有目录service/Media/pictureservice,分析下:

 

关于图片的内容,由PictureService提供

public virtual stringGetDefaultPictureUrl(int targetSize = 0, PictureType defaultPictureType = PictureType.Entity)

        {

…..

   string relPath =_webHelper.GetStoreLocation() + "content/images/"+ defaultImageName;

            if(targetSize == 0)

                returnrelPath;


 

…..

  vardefaultProductPicture = _pictureService.GetPicturesByProductId(product.Id,1).FirstOrDefault();

model.PictureThumbnailUrl= _pictureService.GetPictureUrl(defaultProductPicture, 75, true);

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值