File upload
foreach (string file in Request.Files)
{
HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
if (hpf.ContentLength == 0)
continue;
// string savedFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Path.GetFileName(hpf.FileName));
string savedFileName = Path.Combine(eventImagePath, Path.GetFileName(hpf.FileName));
hpf.SaveAs(savedFileName);
item.Image = Path.GetFileName(hpf.FileName);
}
<div class="form-group">
@Html.LabelFor(model => model.EventTitle, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-6">
@Html.TextBoxFor(model => model.File, new { type = "file" })
@Html.ValidationMessageFor(model => model.EventTitle, "", new { @class = "text-danger" })
</div>
</div>
public class EventViewModel
{
public int EventId { get; set; }
public string EventTitle { get; set; }
public string Location { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public string Details { get; set; }
public string Image { get; set; }
[DataType(DataType.Upload)]
public string File { get; set; }
}