Reputation: 4811
How can i pass 2 word documents to controller from MVC view.
@using (Html.BeginForm("UploadDocument", "Application", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<div class="form-group">
<label for="file1">Please select application document (word file) </label>
<input type="file" name="files" id="file1" class="form-control" />
<label for="file2">Please select support document ( word file )</label>
<input type="file" name="files" id="file2" class="form-control" />
</div>
}
And in controller
public ActionResult UploadDocument(int Id, IEnumerable<HttpPostedFileBase> files)
{
}
But i want to get file1 and file2 differently inside the controller so i can save it to different places. I have to save File1 in one place and file2 in another place. So how can i make sure i get files seperatly?
Upvotes: 1
Views: 71
Reputation: 13997
Give them a separate unique name:
@using (Html.BeginForm("UploadDocument", "Application", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<div class="form-group">
<label for="file1">Please select application document (word file) </label>
<input type="file" name="file1" id="file1" class="form-control" />
<label for="file2">Please select support document ( word file )</label>
<input type="file" name="file2" id="file2" class="form-control" />
</div>
}
controller:
public ActionResult UploadDocument(int Id, HttpPostedFileBase file1, HttpPostedFileBase file2)
{
// do something with the files
}
Even better would be to use strongly typed views with viewmodels:
public class FileUploadViewModel {
HttpPostedFileBase file1 { get; set; }
HttpPostedFileBase file2 { get; set; }
}
view:
@model MyProject.Models.FileUploadViewModel
@using (Html.BeginForm("UploadDocument", "Application", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<div class="form-group">
<label for="file1">Please select application document (word file) </label>
@Html.TextBoxFor(m => m.file1, null, new { type = "file", id = "file1", @class = "form-control" })
<label for="file2">Please select support document ( word file )</label>
@Html.TextBoxFor(m => m.file2, null, new { type = "file", id = "file2", @class = "form-control" })
</div>
}
controller:
public ActionResult UploadDocument(int Id, FileUploadViewModel model)
{
// do something with the files
if(model.file1 != null && model.file1.ContentLength > 0) {
// save first file
}
// etc.
}
Upvotes: 2