0

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; }
}
Brendan Green
  • 11,676
  • 5
  • 44
  • 76
MFogger
  • 1
  • 1
  • sorry, but i can't see using ef?And you can inherited from ApplicationUser and expand your class ImagesUpload. – evilGenius Jun 04 '19 at 18:39
  • Missing a lot of code here that can help others help you. Where is your `DbContext`? Where's the code that does the actual insert into the database? If you are wanting to link the upload to a user, then the email address is redundant, as you'd then `JOIN` between the `ImagesUpload` table and the `AspNetUser` table. – Brendan Green Jun 04 '19 at 21:01
  • @BrendanGreen I've just updated the code so hopefully that may make it clearer. If I need to do a join, that is fine, I just thought I should be able to simply add the email, as I log in using an email address. But that may not be the best way to do it. As I said I'm new to this, so happy to learn best practice. Thanks. – MFogger Jun 04 '19 at 21:21
  • You're not setting the `ApplicationUser` property of `imageUpload` – Brendan Green Jun 04 '19 at 23:12
  • Is that in the controller? If so, do I include it in the Bind ? – MFogger Jun 05 '19 at 15:08
  • Actually, I got there. Thanks for pointing me in the right direction @BrendanGreen. I found this article https://stackoverflow.com/questions/20925822/asp-net-mvc-5-identity-how-to-get-current-applicationuser based on Brendan's response and it gave me the code I needed. Thanks again. – MFogger Jun 05 '19 at 16:46
  • If you've solved the issue, it'd be good to answer your own question, as other users in the future may have the same problem. – Brendan Green Jun 05 '19 at 22:18

0 Answers0