Telerik网格,可以将Id参数添加到列命令中吗?

时间:2013-11-29 11:04:25

标签: c# telerik nopcommerce

C#asp.net 4.5,MS visual studio 2012,nopcommerce CMS和Telerik。

我有以下代码部分:

剃刀查看 ..

<div>
        @(Html.Telerik().Grid<Hroc.Plugin.Misc.ImageGallery.Models.PictureModel.myPictureModel>()
                .Name("productpictures-grid")
                .DataKeys(x =>
                {
                    x.Add(y => y.Id).RouteKey("Id");
                })
                .Columns(columns =>
                {
                    columns.Bound(x => x.PictureUrl)
                        .ClientTemplate("<a href='<#= PictureUrl #>' target='_blank'><img alt='<#= PictureId #>' src='<#= PictureUrl #>' width='150' /><a/>")
                        .ReadOnly();
                    columns.Bound(x => x.DisplayOrder);
                    columns.Bound(x => x.Description);
                    columns.Command(commands =>
                    {
                        commands.Edit().Text(T("Admin.Common.Edit").Text); // can i pass the id here? as well as delete and update?
                        commands.Delete().Text(T("Admin.Common.Delete").Text);
                    });

                })
                .Editable(x =>
                {
                    x.Mode(GridEditMode.InLine);
                })
                .DataBinding(dataBinding =>
                {
                    dataBinding.Ajax().Select("PictureList", "ImageGallery")
                        .Update("GalleryPictureUpdate", "ImageGallery")
                        .Delete("GalleryPictureDelete", "ImageGallery");
                })
                .EnableCustomBinding(true))
</div>

更新删除的控制器部分...

[GridAction(EnableCustomBinding = true)]
    public ActionResult GalleryPictureUpdate(PictureModel.myPictureModel model, GridCommand command)
    {
        var galleryPicture = _galleryItemService.GetGalleryPictureById(model.Id);//get selected id?
        if (galleryPicture == null)
            throw new ArgumentException("No product picture found with the specified id");

        galleryPicture.OrderNumber = model.DisplayOrder;
        _galleryItemService.UpdateGallerytPicture(galleryPicture);

        return PictureList(command);
    }

    [GridAction(EnableCustomBinding = true)]
    public ActionResult GalleryPictureDelete(PictureModel.myPictureModel model, GridCommand command)
    {
        var galleryPicture = _galleryItemService.GetGalleryPictureById(model.Id);
        if (galleryPicture == null)
            throw new ArgumentException("No product picture found with the specified id");

        var pictureId = galleryPicture.PictureID;

        _galleryItemService.DeleteProductPicture(galleryPicture);
        var picture = _pictureService.GetPictureById(pictureId);
        _pictureService.DeletePicture(picture);

        return PictureList(command);
    }

我的服务类部分...

 public virtual GalleryItem GetGalleryPictureById(int galleryPictureId)
    {
        if (galleryPictureId == 0)
            return null;

        return _ImageItemRepository.GetById(galleryPictureId);
    }

    public virtual void UpdateGallerytPicture(GalleryItem galleryPicture)
    {
        if (galleryPicture == null)
            throw new ArgumentNullException("galleryPicture");

        _ImageItemRepository.Update(galleryPicture);

        //event notification
        _eventPublisher.EntityUpdated(galleryPicture);
    }

    public virtual void DeleteProductPicture(GalleryItem galleryPicture)
    {
        if (galleryPicture == null)
            throw new ArgumentNullException("productPicture");

        _ImageItemRepository.Delete(galleryPicture);

        //event notification
        _eventPublisher.EntityDeleted(galleryPicture);
    }

从提供的代码中,它全部位于nopcommerce中的新插件(类库)中。

基本上,当您配置插件部分时,显示的视图显示图像列表,然后您可以将新图像上载到列表中,然后显示该列表。这部分工作正常。

“编辑”命令允许Telerik命令选项起作用,因此您可以更改显示顺序和说明。但是,当您要更新或删除其抛出时,异常“找不到ID”。

这里有什么我想念的吗?我相信它,因为我实际上并没有获得特定的ID并将其传递给我的控制器方法(发生错误的地方)。

任何帮助都会很棒!

更新:02/12/2013按要求获取代码以获取图像列表。

控制器

[HttpPost, GridAction(EnableCustomBinding = true)]
    public ActionResult PictureList(GridCommand command)
    {
        if (!_permissionService.Authorize(StandardPermissionProvider.ManagePlugins))
            return Content("Access denied");

        var nop_Image = _galleryItemService.Fetch(); //my image

        var Nop_ImagesModel = nop_Image
            .Select(x =>
            {
                var nop_gModel = new PictureModel.myPictureModel()
                {
                    PictureId = x.Id,
                    PictureUrl = _pictureService.GetPictureUrl(x.PictureID),
                    DisplayOrder = x.OrderNumber,
                    Description = x.Description
                };

                return nop_gModel;
            })
            .ToList();

        var model = new GridModel<PictureModel.myPictureModel>
        {
            Data = Nop_ImagesModel, 
        };

        return new JsonResult
        {
            Data = model
        };
    }

根据要求更新:2013年12月2日

[GridAction(EnableCustomBinding = true)]
    public ActionResult GalleryPictureDelete(PictureModel.myPictureModel model, GridCommand command)
    {
        var galleryPicture = _galleryItemService.GetGalleryPictureById(model.PictureId);
        if (galleryPicture == null)
            throw new ArgumentException("No product picture found with the specified id");

        var pictureId = galleryPicture.PictureID;

        _galleryItemService.DeleteProductPicture(galleryPicture);
        var picture = _pictureService.GetPictureById(pictureId);
        _pictureService.DeletePicture(picture);

        return PictureList(command);
    }

视图

<div>
             @(Html.Telerik().Grid<@Hroc.Plugin.Misc.ImageGallery.Models.PictureModel.myPictureModel>()
                .Name("productpictures-grid")
                .DataKeys(x =>
                {
                    x.Add(y => y.Id).RouteKey("Id");
                })
                .Columns(columns =>
                {
                    columns.Bound(x => x.PictureId).Hidden(true); 
                    columns.Bound(x => x.PictureUrl)
                        .ClientTemplate("<a href='<#= PictureUrl #>' target='_blank'>    <img alt='<#= PictureId #>' src='<#= PictureUrl #>' width='150' /><a/>")
                        .ReadOnly();
                    columns.Bound(x => x.DisplayOrder);
                    columns.Bound(x => x.Description);
                    columns.Command(commands =>
                    {
                        commands.Edit().Text(T("Admin.Common.Edit").Text); 
                        commands.Delete().Text(T("Admin.Common.Delete").Text);
                    });
                })

                .Editable(x =>
                {
                    x.Mode(GridEditMode.InLine);
                })
                .DataBinding(dataBinding =>
                {
                    dataBinding.Ajax().Select("PictureList", "ImageGallery")
                        .Update("GalleryPictureUpdate", "ImageGallery")
                        .Delete("GalleryPictureDelete", "ImageGallery");
                })
                .EnableCustomBinding(true))
</div>

1 个答案:

答案 0 :(得分:2)

您唯一需要做的就是分配Id而不是pictureId并保留您的第一个代码

.DataKeys(x =>
                {
                    x.Add(y => y.Id).RouteKey("Id");
                })



 var nop_gModel = new PictureModel.myPictureModel()
                {
                    Id = x.Id,//Changed as you are getting picture by Id
                    PictureUrl = _pictureService.GetPictureUrl(x.PictureID),
                    DisplayOrder = x.OrderNumber,
                    Description = x.Description
                };



var galleryPicture = _galleryItemService.GetGalleryPictureById(model.Id);