I'm relatively new to MVC, and I've never had to deal with uploading a file (an image, specifically) to a SQL Server database. To be honest, I don't know what I'm doing here.
Here's what I have so far - here's my domain model (note the HttpPostedFileBase
in my model - this is what I want to upload):
public class Profile
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage="Years of service is required")]
[DisplayName("Years Service:")]
public int YearsService { get; set; }
[DataType(DataType.MultilineText)]
[DisplayName("Notable Achivements:")]
public string NotableAchivements { get; set; }
[Required(ErrorMessage = "Technical skills are required")]
[DataType(DataType.MultilineText)]
[DisplayName("Technical Skills:")]
public string TechnicalSkills { get; set; }
[DisplayName("Upload Image: ")]
public HttpPostedFileBase Photo { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
public string ModifiedBy { get; set; }
public DateTime ModifiedDate { get; set; }
}
And here's my view:
@using (Html.BeginForm("Create", "Profiles", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="editor-label">
@Html.LabelFor(model => model.YearsService)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.YearsService)
@Html.ValidationMessageFor(model => model.YearsService)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.NotableAchivements)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NotableAchivements)
@Html.ValidationMessageFor(model => model.NotableAchivements)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.TechnicalSkills)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.TechnicalSkills)
@Html.ValidationMessageFor(model => model.TechnicalSkills)
</div>
<input type="file" name="photo" />
<input type="submit" name="Submit" id="Submit" value="Upload" />
}
I hope there's something glaringly obvious that I'm doing wrong. Can anyone provide advice on how to do a simple file upload to a SQL Server database?