添加颜色和尺码管理菜单(配图是网友使用的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("<imgaltimgalt='<#= 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);