I'm writing an app using VS2019 EF. I would like to be able to upload images and store them in a table in my LocalDB (for now). I want to use the email address from the AspNetUsers
table to identify who has uploaded what image.
Also, I have tried to include the Id from that table as the foreign key in my new table, not sure where it is gone wrong, but the Id is not loaded into my Images table (problem 1). Also I'm trying to store the email address of the logged in user in this new table so I can identify the images later. The current user is being saved to the dB, but when I use the EF Index View page to give me a list of the images, it replaces the email from the database with the current logged in user (admin as this is an admin only page).
This is my class:
public class ImagesUpload
{
// ApplicationUser user = new ApplicationUser();
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ImagesUploadID { get; set; }
public string Id{ get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
//(supposed to set the foreign key, but nothing is loaded into the db)
private string email;
public string Email
{
get
{
return email = HttpContext.Current.User.Identity.Name;
}
//set
//{
// email = value;
//}
}
[Required]
[Display(Name ="Select your image type ")]
public string ImageType { get; set; }
Here is my Index View code:
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Email)
</td>
<td>
@Html.DisplayFor(modelItem => item.ImageType)
</td>
This is my local database table:
ImageID Id Title Path Score ImageType Email
1024 NULL ladybug ~/Images/Colour/ASurfersPlayground.jpg 0 Colour maria@mail.com
1025 NULL test ~/Images/Mono/Butterfly.jpg 0 Mono maria@mail.com
1026 NULL test ~/Images/Theme/ladybug.jpg 0 Theme NULL
1027 NULL ladybug ~/Images/Mono/Butterfly.jpg 0 Mono maria@mail.com
NULL NULL NULL NULL NULL NULL NULL
The index view is as follows (sorry only letting me copy text):
Index
Create New
Email Select your image type Image 1 Title Select Image 1
admin@mail.com Mono testing ~/Images/Mono/southwall.jpg
From debugging I know the email address from the db is overwritten as I'm using HttpContext.Current.User.Identity.Name in the model, but I don't know how else to get the email into the db for each user.
Apologies, here is the controller data
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "image1Title,image1Path, imageType")]ImagesUpload imagesUpload, HttpPostedFileBase Image1File)
{
try
{
int colour = 0;
int mono = 0;
int theme = 0;
if (ModelState.IsValid)
{
switch (imagesUpload.ImageType)
{
case "Colour":
{
if (colour < 2)
{
if (Image1File != null && Image1File.ContentLength > 0)
{
string colour1filename = Path.GetFileNameWithoutExtension(Image1File.FileName);
string colour1extension = Path.GetExtension(Image1File.FileName);
colour1filename = colour1filename + DateTime.Now.ToString("yymmssfff") + colour1extension;
imagesUpload.Image1Path = "~/Images/Colour/" + colour1filename;
colour1filename = Path.Combine(Server.MapPath("~/Images/Colour/"), colour1filename);
Image1File.SaveAs(colour1filename);
db.ImagesUploads.Add(imagesUpload);
colour++;
db.SaveChanges();
}
}
else
{
ViewBag.Message = "You have already uploaded two colour images";
}
break;
}
case "Mono":
{
if (mono < 2)
{
if (Image1File != null && Image1File.ContentLength > 0)
{
string mono1filename = Path.GetFileNameWithoutExtension(Image1File.FileName);
string mono1extension = Path.GetExtension(Image1File.FileName);
mono1filename = mono1filename + DateTime.Now.ToString("yymmssfff") + mono1extension;
imagesUpload.Image1Path = "~/Images/Mono/" + mono1filename;
mono1filename = Path.Combine(Server.MapPath("~/Images/Mono/"), mono1filename);
Image1File.SaveAs(mono1filename);
db.ImagesUploads.Add(imagesUpload);
mono++;
db.SaveChanges();
}
}
else
{
ViewBag.Message = "You have already uploaded two mono images";
}
break;
}
case "Theme":
{
if (theme < 2)
{
if (Image1File != null && Image1File.ContentLength > 0)
{
string theme1filename = Path.GetFileNameWithoutExtension(Image1File.FileName);
string theme1extension = Path.GetExtension(Image1File.FileName);
theme1filename = theme1filename + DateTime.Now.ToString("yymmssfff") + theme1extension;
imagesUpload.Image1Path = "~/Images/Theme/" + theme1filename;
theme1filename = Path.Combine(Server.MapPath("~/Images/Theme/"), theme1filename);
Image1File.SaveAs(theme1filename);
db.ImagesUploads.Add(imagesUpload);
theme++;
db.SaveChanges();
}
}
else
{
ViewBag.Message = "You have already uploaded two theme images";
}
break;
}
}
return RedirectToAction("AnotherImage");
}
and my dbcontext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public System.Data.Entity.DbSet<B8IT130_10374500.Models.ImagesUpload> ImagesUploads { get; set; }
public System.Data.Entity.DbSet<B8IT130_10374500.Models.Contact> Contacts { get; set; }
}