0

I want to show the images stored in my database in my View.
I have a field "deliverable_image" from the type "longblob" in my mysql database.

In my controller:

public ActionResult Show( int id )
    {
        var imageData = repository.GetAllImages();

        return File( imageData, "image/jpg" );
    }

Repository:

public IQueryable<byte[]> GetAllImages()
    {
        return from deliverable in entities.deliverables
               orderby deliverable.deliverable_image
               select deliverable.deliverable_image;
    }

View:

<img src='<%= Url.Action( "Show", "Deliverable", new { id = ViewData["DeliverableID"] } ) %>' />

But in my controller I get the error:

cannot convert from 'System.Linq.IQueryable' to 'string'

Does anybody knows how I can fix this?

1 Answer 1

1

In your action method, the very first line gets a collection of images (specifically, an IQueryable<byte[]>:

var imageData = repository.GetAllImages();

Then you try to send all of the images as a single file:

return File(imageData, "image/jpg");

imageData is a collection of files. You need to select one of them and return just that. To do that, you'll probably need to modify your GetAllImages function or use a different function. The action method is being passed an id, but you can't use that on the results of that function because all you have are byte arrays. You need to filter for the id specified and then use the resulting byte array.

Sign up to request clarification or add additional context in comments.

2 Comments

Do you know how I can do this in my repository?
@niels123: You can either pass the id to the repository function and add a where clause to the LINQ statement, or return more than just the byte array (an object, basically) from the repository and filter with a .Where() when calling the repository function. I personally prefer the latter, as it makes sense to me that a repository is a place to get and persist objects, not just pieces of data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.