I have multiple file upload views with view model binding as following
@model IVRControlPanel.Models.UploadNewsModel
@using (Html.BeginForm("index", "NewsUpload", FormMethod.Post, new { name = "form1", @id = "form1" }))
{
@Html.ValidationSummary(true)
<div class="field fullwidth">
<label for="text-input-normal">
@Html.Label("Select Active Date Time")</label>
<input type="text" id="active" value="@DateTime.Now" />
@Html.ValidationMessageFor(model => model.ActiveDateTime)
</div>
<div class="field fullwidth">
<label>
@Html.Label("Select Language")
</label>
@Html.DropDownList("Language", (SelectList)ViewBag.lang)
</div>
<div class="field">
<label>
@Html.Label("General News")
</label>
@Html.TextBoxFor(model => model.generalnews, new { name = "files", @class="custom-file-input", type = "file" })
@Html.ValidationMessageFor(model => model.generalnews)
</div>
<div class="field">
<label>
@Html.Label("Sports News")
</label>
@Html.TextBoxFor(model => model.sportsnews, new { name = "files", @class = "custom-file-input", type = "file" })
@Html.ValidationMessageFor(model => model.sportsnews)
</div>
<div class="field">
<label>
@Html.Label("Business News")
</label>
@Html.TextBoxFor(model => model.businessnews, new { name = "files", @class = "custom-file-input", type = "file" })
@Html.ValidationMessageFor(model => model.businessnews)
</div>
<div class="field">
<label>
@Html.Label("International News")
</label>
@Html.TextBoxFor(model => model.internationalnews, new { name = "files", @class = "custom-file-input", type = "file" })
@Html.ValidationMessageFor(model => model.internationalnews)
</div>
<div class="field">
<label>
@Html.Label("Entertaintment News")
</label>
@Html.TextBoxFor(model => model.entertaintmentnews, new { name = "files", @class = "custom-file-input", type = "file" })
@Html.ValidationMessageFor(model => model.entertaintmentnews)
</div>
<footer class="pane">
<input type="submit" class="bt blue" value="Submit" />
</footer>
}
View model with data annotation for validating file upload for allowed extension as follows:
public class UploadNewsModel
{
public DateTime ActiveDateTime { get; set; }
// public IEnumerable<SelectListItem> Language { get; set; }
[File(AllowedFileExtensions = new string[] { ".jpg", ".gif", ".tiff", ".png", ".pdf", ".wav" }, MaxContentLength = 1024 * 1024 * 8, ErrorMessage = "Invalid File")]
public HttpPostedFileBase files { get; set; }
}
Controller: for saving multiple file and return view if error is exist
[HttpPost]
public ActionResult Index(UploadNewsModel news, IEnumerable<HttpPostedFileBase> files)
{
if (ModelState.IsValid)
{
foreach (var file in files)
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var serverpath = Server.MapPath("~/App_Data/uploads/News");
var path = Path.Combine(serverpath, fileName);
if (!Directory.Exists(serverpath))
{
Directory.CreateDirectory(serverpath);
}
file.SaveAs(path);
}
}
}
return View(news);
}
}
Problem explaination How do I define view model for those five file upload input control so that corresponding error is shown for respective validation error if file extension of file uploaded is not allowed type. I have only one view model items for all five file upload control.
What can be best way to define view model for those multiple file upload control for showing respective validation error instead user try to upload file of unallowed extension???