【问题标题】:Telerik grid, add Id parameter into column commands possible?Telerik网格,可以将Id参数添加到列命令中吗?
【发布时间】:2013-11-29 11:04:25
【问题描述】:

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 并将其传递给我的控制器方法(发生错误的地方)。

任何帮助都会很棒!

更新:2013 年 2 月 12 日按要求提供获取图像列表的代码。

控制器

[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 年 2 月 12 日

[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>

【问题讨论】:

  • 请提供获取图片列表的代码

标签: c# telerik nopcommerce


【解决方案1】:

您只需分配 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);

【讨论】:

  • 很抱歉,这没有影响
  • 在更新记录时它到底在哪里抛出错误?它是否达到断点。 ModelState 中有哪些错误?检查更新
  • 成功了,更新现在可以工作了,但是对于删除操作却没有这样说。为 id 拉回 0
  • 你是否对删除做了同样的事情,而不是在你的情况下是它的 PictureId
  • 是的,ID 恢复为 0,我相信这是由于 id 没有被列表拉回​​span>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-21
  • 2015-09-23
  • 2011-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-20
相关资源
最近更新 更多