I have 3 form inputs for 3 files and I will have to save each of them to separate columns in the database(sqlserver). I am not able to figure out how to add each filename to a separate column. Please help. I am trying to achieve the commented out code in my controller but I am not sure what the syntax would be. My code is as follows:
View:
@Html.TextBoxFor(model => model.SerialAttachment, new { type = "file", name = "file1", id="file1" })
@Html.TextBoxFor(model => model.CountryAttachment, new { type = "file", name = "file2", id = "file2" })
@Html.TextBoxFor(model => model.OtherAttachment, new { type = "file", name = "file3", id = "file3" })
Controller:
`public ActionResult Create([Bind(Include = "Id,Serial,PinNumbers,SerialAttachment,CountryAttachment,OtherAttachment")] ModelName modelInstance`) {
if (Request.Files.Count > 0)
{
for (int i = 0; i < Request.Files.Count; i++)
{
var fileUp = Request.Files[i];
if (fileUp != null && fileUp.ContentLength > 0)
{
var fname = Path.GetFileName(fileUp.FileName);
var path = Path.Combine(Server.MapPath(fname));
fileUp.SaveAs(path);
// modelInstance.SerialAttachment = fname;
// modelInstance.CountryAttachment = fname;
// modelInstance.OtherAttachment = fname;
db.model.Add(modelInstance);
db.SaveChanges();
}
}
return RedirectToAction("Index");
}
Model:
public partial class ModelName
{
public int? Serial { get; set; }
public int? PinNumbers { get; set; }
[StringLength(250)]
public string SerialAttachment { get; set; }
[StringLength(250)]
public string CountryAttachment { get; set; }
[StringLength(250)]
public string OtherAttachment { get; set; }
}