I am trying to allow users to select an image and add some additional information to it.
I have an object that holds the additional info – lets just keep it simple and say its two strings – and an image
[Table("ImageInfo")]
public class ImageInfo
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ImageInfoId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Image Image { get; set; }
}
[Table("Image")]
public class Image
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int IamgeId { get; set; }
public byte[] ImageBytes { get; set; }
}
For the Create view I want to show the users two text fields and a ‘gallary’ type layout of all existing iamges from the database.
I would like the iamge gallary part to be a partial view so I can reuse it elsewhere too.
I cannot figure out how to display the images and be able to see which one a use has selected in the controller to link them up. I imaging it will require some view specific models but beyond that I am drawing a blank.
Can someone point me in the right direction please.
ps. reason for storing ImageInfo and Image in seperate tables is that there will be many ImageInfo for each Image.
Yes. you need some view specific models. We will call it ViewModels. They are simple POCO classes to be used in the View. so let’s create 2 of them for our scenario.
Now in the
GETaction, set the property values on an object of our viewmodelNow our View will be strongly typed to our viewmodel
AvailableImageswill be a partial view which is strongly typed to a collection ofImageViewModelAdd some jquery code, so that when user selects an image, get the image id and set it to the Hidden variable(
SelectedImageID) in our form so that it will be available when the form is posted.