Mode:-
public class Patner
{
[Key]
public int PatnerId { get; set; }
[Required(ErrorMessage = "Patner name is required")]
[Display(Name="Patner Name: ")]
public string PatnerName { get; set; }
[Display(Name = "Id Proof: ")]
public string IdProof { get; set; }
[Display(Name = "Address Proof: ")]
public string AddressProof { get; set; }
}
View:-
@using (Html.BeginForm("Patner", "Home", FormMethod.Post, new { enctype = "multipart/form-data"}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<p>
@Html.LabelFor(model => model.PatnerName, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.PatnerName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PatnerName, "", new { @class = "text-danger" })
</p>
<p>
@Html.LabelFor(model => model.IdProof, htmlAttributes: new { @class = "control-label col-md-2" })
<input type="file" name="file[]" id="file" accept=".doc" />
</p>
<p>
@Html.LabelFor(model => model.AddressProof, htmlAttributes: new { @class = "control-label col-md-2" })
<input type="file" name="file[]" id="file" accept=".doc" />
</p>
<p class="stdformbutton">
<input type="submit" value="Save" id="btnsubmit" class="btn btn-default" />
<input type="button" value="Cancel" onclick="location.href='@Url.Action("Patner","Home")';" class="btn" />
</p>
}
Controller :-
[HttpPost]
public ActionResult Patner(Patner patner, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
string path = Path.Combine(Server.MapPath("~/Upload"), Path.GetFileName(file.FileName));
file.SaveAs(path);
using (SolarDbContext db = new SolarDbContext())
{
patner.IdProof = file.FileName;
patner.AddressProof= ?
db.Patners.Add(patner);
db.SaveChanges();
}
ModelState.Clear();
ViewBag.Message = "Data successfully saved.";
}
return View();
}
How to upload both file with data?
Thanks
Manamohan