6

I'm trying to render a image field with RenderImage. I need some data- attributes in the image but I can't seem to figure out how to go about implementing that. I've tried this but doesn't work

@RenderImage(image, x => x.Image, new RenderingParameters("data-protect=true"), isEditable: true)   

Thanks

Yan Sklyarenko
  • 31,557
  • 24
  • 104
  • 139
Gabbar
  • 4,006
  • 7
  • 41
  • 78

2 Answers2

9

Although the answer above will work I am going to be removing ImageParameters support in the future and moving to just anonymous type support:

@RenderImage(image, x => x.Image, new { Width = 100}, isEditable: true)

The reason for this change is because having a strongly type class like ImageParameters is very limiting. Anonymous types are also a common way of doing this with other frameworks so it would fit with what everyone else is doing.

Updated to include rendering of data attributes:

@RenderImage(image, x => x.Image, new { data_protect = "true"}, isEditable: true)
nologo
  • 5,918
  • 3
  • 36
  • 50
Michael Edwards
  • 6,308
  • 6
  • 44
  • 75
  • 1
    The issue was with data-protect that I haven't been able to get to work. So if you can add support for hyphen named attributes would be great. – Gabbar Oct 21 '14 at 17:12
  • 4
    Gabbar - the latest nightly release has this resolved. You can use "data_id" to get "data-id" in the same way as MVC. – Michael Edwards Oct 24 '14 at 07:14
6

Try it like this:

@RenderImage(image, x => x.Image, new ImageParameters { Width = 100}, isEditable: true)

As of recent version of Glass - the only possible solution is

@RenderImage(image, x => x.Image, new { Width = 100}, isEditable: true)

Also you can take a look at - TUTORIAL 16 - RENDERING IMAGES

nsgocev
  • 4,390
  • 28
  • 37
  • I've edited my question. The data-protect is a custom attribute. How can I get that to show up? – Gabbar Oct 20 '14 at 18:33
  • @Gabbar I don`t think it is possible without creating a new class than extends from AbstractParameters. You can check the default ImageParameters here. Just implement your custom class with the data required. https://github.com/mikeedwards83/Glass.Mapper/blob/master/Source/Glass.Mapper.Sc/RenderField/ImageParameters.cs. Another option is to render the image manually (using the src property) and show it as editable only when in page editor mode. – nsgocev Oct 20 '14 at 19:06
  • Ok that makes sense. I'll try both approaches. – Gabbar Oct 20 '14 at 19:13