C# Telerik grid,是否可以在列命令中添加Id参数?

C# Telerik grid,是否可以在列命令中添加Id参数?,c#,telerik,nopcommerce,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")

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>
我的服务班级部分

 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”

这里有我遗漏的东西吗?我相信这是因为我并没有真正获得特定的ID并将其传递给我的控制器方法(错误发生的地方)

任何帮助都会很好

更新日期:2013年12月2日根据要求,以下是获取图像列表的代码

控制器

[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
        };
    }
景色

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

@(Html.Telerik().Grid())
.Name(“产品图片网格”)
.DataKeys(x=>
{
x、 添加(y=>y.Id).RouteKey(“Id”);
})
.列(列=>
{
columns.Bound(x=>x.PictureId).Hidden(true);
columns.Bound(x=>x.PictureUrl)
.ClientTemplate(“'width='150'/>”)
.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);
});
})
.可编辑(x=>
{
x、 模式(GridEditMode.InLine);
})
.DataBinding(数据绑定=>
{
Ajax().Select(“PictureList”、“ImageGallery”)
.更新(“GalleryPictureUpdate”、“ImageGallery”)
.删除(“GalleryPictureDelete”、“ImageGallery”);
})
.EnableCustomBinding(true))

您需要做的唯一一件事是分配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);

请发布获取ImagesOrry列表的代码。在更新记录时,这与抛出错误的确切位置没有区别?它是否达到断点。ModelState中有哪些错误?Check updatethat't完成了这个技巧,更新现在可以工作了,但是对于删除操作来说,情况并非如此。为Id调回0是否为删除做了相同的操作,而不是Id在您的案例中的图片Id是的,Id返回0,我相信这是由于列表没有调回Id
<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>
.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);