1 Replies - 2691 Views - Last Post: 05 December 2011 - 08:03 PM

#1 antarr.byrd  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 247
  • Joined: 30-August 09

How do upload and associate an image with another class simultaneously

Posted 01 December 2011 - 07:36 PM

I have a class, Event, and i want to be able to have images on the event page. I have defined the image class but am now sure how i can upload the image. I want to be able to store the image in the database.

public class Event
    {
        public int Id { get; set; }

        public string Title { get; set; }

        public string  Description { get; set; }

        public string Address { get; set; }

        public string AddressTwo { get; set; }

        public virtual Person Owner { get; set; }

        public DateTime Date { get; set; }

        public virtual Image Image { get; set; }
    }

 public class Image 
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public string AlternateText { get; set; }

        public virtual string CssClass { get; set; }

        public Byte[] File { get; set; }
    }


Is This A Good Question/Topic? 0
  • +

Replies To: How do upload and associate an image with another class simultaneously

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: How do upload and associate an image with another class simultaneously

Posted 05 December 2011 - 08:03 PM

Do something like this:

<form action="Upload" enctype="multipart/form-data" method="post">
    <input type="file" name="image">
    <input type="submit" value"Save">
</form>


[HttpPost]
public ActionResult Upload(HttpPostedFileBase image)
{
    if (image != null) {
        // do something with the image.
    }
    return View();
}



You submit the image in a form POST, then handle it in the resulting action method.

This post has been edited by Sergio Tapia: 05 December 2011 - 08:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1