Hello Guys
OK Quick Overview
Class Employee like Below + some extra supporting classes
But the focus of my problem is on
public virtual byte[] EmployeePhoto {get; set;}
part
First I was looking to just Upload whole image to DB and later retrieve it back to view from DB but don't know why its look hard for me like I'm new to this that's why i Chose other way
Upload File to folder on the server and path + file name as a string to DB don't know why looks easier for me.
public class Employee
{
[Key]
public virtual int EmployeeID { get; set; }
[ForeignKey("TitleID")]
public virtual Title Title { get; set; }
[Display(Name = "Title")]
public virtual int TitleID { get; set; }
[Display(Name = "First Name")]
[Required]
[StringLength(50, ErrorMessage = "First name cannot be longer than 50 characters.")]
public virtual string FirstName { get; set; }
[Display(Name = "Last Name")]
[Required]
[StringLength(50, ErrorMessage = "Last name cannot be longer than 50 characters.")]
public virtual string LastName { get; set; }
[Display(Name = "Full Name")]
public virtual string FullName
{
get
{
return LastName + ", " + FirstName;
}
}
[Display(Name = "Date of Birth")]
[DataType(DataType.Date)]
[DisplayFormat(NullDisplayText = "", DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public virtual DateTime? DOB { get; set; }
[ForeignKey("GenderID")]
public virtual Gender Gender { get; set; }
[Display(Name = "Gender")]
public virtual int GenderID { get; set; }
[Display(Name = "Adress Line 1")]
public virtual string AddressLine1 { get; set; }
[Display(Name = "Adress Line 2")]
public virtual string AddressLine2 { get; set; }
public virtual string City { get; set; }
[Display(Name = "Post Code")]
public virtual string PostCode { get; set; }
[Display(Name = "Email")]
public virtual string EmailAddress { get; set; }
[Display(Name = "Phone Nr")]
public virtual int Phone { get; set; }
[Display(Name = "Employment Nr")]
public virtual int EmploymentNr { get; set; }
[Display(Name = "Start Date")]
[Required]
[DataType(DataType.Date)]
[DisplayFormat(NullDisplayText = "" , DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public virtual DateTime? HireDate { get; set; }
[Display(Name = "Holiday Total Allowed")]
public virtual int HolidayTotal { get; set; }
[Display(Name = "Sick Leave Total Allowed")]
public virtual int SickLeaveTotal { get; set; }
[ForeignKey("ShiftID")]
public virtual Shift Shift { get; set; }
[Required]
[Display(Name = "Shift")]
public virtual int ShiftID { get; set; }
public virtual byte[] EmployeePhoto { get; set; }
}
public class Title
{
public virtual int TitleID { get; set; }
[Column("Title")]
[Display(Name = "Title")]
[Required]
public virtual string Name { get; set; }
}
public class Gender
{
public virtual int GenderID { get; set; }
[Column("Gender")]
[Display(Name = "Gender")]
[Required]
public virtual string Name { get; set; }
}
public class Shift
{
public virtual int ShiftID { get; set; }
[Column("Shift")]
[Display(Name = "Shift")]
[Required]
public virtual string Name { get; set; }
[Display(Name = "Start Time")]
[Required]
[DataType(DataType.Time)]
[DisplayFormat(NullDisplayText = "", DataFormatString = "{0:hh:mm}", ApplyFormatInEditMode = true)]
public virtual DateTime StartTime { get; set; }
[Display(Name = "End Time")]
[Required]
[DataType(DataType.Time)]
[DisplayFormat(NullDisplayText = "", DataFormatString = "{0:hh:mm}", ApplyFormatInEditMode = true)]
public virtual DateTime EndTime { get; set; }
}
}
I think I need to change
public virtual byte[] EmployeePhoto {get; set;}
to
public virtual string Photo {get; set;}
Then Edit the View Model for Create Employee, take part of Model.Photo
And add
<form action="Categories/Upload" enctype="multipart/form-data" method="post">
<input type="file" name="Image">
<input type="submit" value"Save">
</form>
Then I need to edit my Controller Adding
Something like
[HttpPost]
public ActionResult Upload(string ActionName)
{
var path = Server.MapPath("~/App_Data/Files");
foreach (string item in Request.Files)
{
HttpPostedFileBase file = Request.Files[item];
if (file.ContentLength == 0)
{
continue;
}
string savedFileName = Path.Combine(path, Path.GetFileName(file.FileName));
file.SaveAs(savedFileName);
}
return RedirectToAction(ActionName);
}
but I don't Know how to Perform saving of the file path as a string to my DB Photo Column
that's the issue and that's the problem i need some help
also i have a costume Html helper to retrieve the whole thing later from
public static class CustomHtmlHelper
{
public static IHtmlString Image(this HtmlHelper helper, string src, string alt)
{
TagBuilder tb = new TagBuilder("img");
tb.Attributes.Add("src", VirtualPathUtility.ToAbsolute(src));
tb.Attributes.Add("alt", alt);
return new MvcHtmlString(tb.ToString(TagRenderMode.SelfClosing));
}
}
result will be
@Html.Image(Model.Photo, Model.AlternateText)
What will return Photo in my detiles view model
I'm also open for other solutions as I'm learning and this may be a wrong approach
Best Regards