I am trying to allow only excel files (.xls and .xlsx) in my asp.net mvc application but when i try to upload excel file (in my case 'sale.xlsx'), it shown false for ModelState.IsValid. What can be the problem.
Below are the codes.
Model:
public class ReadExcel
{
[Required(ErrorMessage = "Please select file")]
[FileExtensions(Extensions = ".xls,.xlsx", ErrorMessage = "Only excel file")]
public HttpPostedFileBase file { get; set; }
}
View:
@using (Html.BeginForm("Index", "ReadExcel", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.TextBoxFor(m => m.file, new { type = "file" })
<button id="submitButton" type="submit">Submit</button>
@Html.ValidationMessageFor(model => model.file)
}
Controller:
[HttpPost]
public ActionResult Index(ReadExcel readExcel)
{
if (ModelState.IsValid)
{
//code
}
return View();
}
Clearly when uploading excel the control should move inside the if block (if (ModelState.IsValid)). But it is not. please help?