1

For an ongoing project i have (amongst other classes) the following:

public class Page
    {
        [Key]
        public int PageId { get; set; }
        public string Name { get; set; } //eg. "AboutUs", "Location"
        [Column(TypeName = "ntext")]  //force Entity Framework to create a ntext column
        public string Title { get; set; }
        public string Subtitle { get; set; }
        public string Content { get; set; }



        //navigational properties
        public virtual ObservableCollection<Image> Images{ get; set; } //one  Page has many Images

        public Page()
        {
            Images= new ObservableCollection<Image>();
        }
    }

I'm using Entity Framework code first approach in this ASP MVC 3 project (using Razor) and do not have any problem inserting and updating objects of this type.

BUT: how can i have a master detail view in which the detail part is composed by images only (see class definition). So how is it possible to add an image, if the user doesn't want it to have it deleted and of course how to show all the images in a list?

Any hint is deeply appreciated!

Savvas Sopiadis
  • 8,213
  • 10
  • 34
  • 53

1 Answers1

0

Look at this post: Display image from database in asp mvc

If you are trying to render from the database, create a method in your controller to get the image and use that as your image source like in the example above. The only difference is yours will be contained in a

<ul>
@foreach(var image in Model.Images) {
    <li><a href="#"><img src="@Url.Action("Show", "Image", new {id = image.Id})" /></a></li>
}
</ul>

Then your model would contain the Id's of the images, and it would be the job of the action method to retrieve the image.

Community
  • 1
  • 1
shuniar
  • 2,592
  • 6
  • 31
  • 38
  • :i think that this is not possible since the image is stored in the dbms. So there is no possibility to have a URL (one solution would be to store the image on the file system, than this approach would work) – Savvas Sopiadis Oct 04 '11 at 20:40
  • Take a look at this post [here](http://stackoverflow.com/questions/880515/display-image-from-database-in-asp-mvc). – shuniar Oct 04 '11 at 21:17
  • this solves the problem ... partly!This solution is based on the fact that the image is (somehow) already stored in the dbms.The requirement i have is: the user can add & delete images and finally do the actual saving of the page object (which includes the pictures). This means that she could decide not to save the page object, which means that the pictures shouldn't exist in the dbms, but due to the fact of the solution they will already have been stored. – Savvas Sopiadis Oct 04 '11 at 21:35
  • Then instead of loading from the database keep a collection of `File` objects instead of `Image` objects and do the same thing. – shuniar Oct 04 '11 at 23:34