I've found some code to do this and tried to implement it into my project, but so far it has been unsuccessful. I don't get any errors, but I don't see any images being stored in my images directory inside visual studio.
View:
@using (Html.BeginForm())
{
<span>Please enter your story here:</span>
<textarea id="testimonial" name="testimonial"></textarea>
<button type="submit">Submit</button>
<input type="file" name="file" />
}
Controller:
[HttpPost]
public ActionResult Create(Testimonials testimonials)
{
if (Request.Files.Count > 0)
{
var file = Request.Files[0];
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Images/"), fileName);
file.SaveAs(path);
}
}
TestimonialsContext testContext = new TestimonialsContext();
testContext.testimonialContext.Add(testimonials);
testContext.SaveChanges();
return RedirectToAction("Index");
}
The part below the if block works fine. That just saves the content of a textarea to the database. Any thoughts? Do I need to make any changes to my model?
model:
[Table("Testimonials")]
public class Testimonials
{
public int Id { get; set; }
public string Testimonial { get; set; }
}
context class:
public class TestimonialsContext:DbContext
{
public DbSet<Testimonials> testimonialContext { get; set; }
}