0

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?

yogihosting
  • 5,494
  • 8
  • 47
  • 80
  • What is your `FileExtensionsAttribute`? –  May 13 '17 at 13:06
  • fileextension is not mine it comes by default. Provided by mvc just like [Required] – yogihosting May 13 '17 at 13:08
  • I just googled and read that the type for the property may need to be of type `string` - can you try that? – Mark C. May 13 '17 at 13:11
  • From where? If `ModelState` is invalid and that is the only property in your model, then clearly there is a problem with the code in that validation attribute. You need to show the code for it (its not part of MVC). And have you confirmed that the error is for property `file` –  May 13 '17 at 13:11
  • @MarkC. you are right it needs to be string. See here http://stackoverflow.com/a/8536682/4868839 – User3250 May 13 '17 at 13:11
  • thank you @MarkC. for telling me it needs to be string for [FileExtensions]. So i think i should use [RegularExpression] instead of it since i don't want to change by model code. But regularexpression too is failing for model property 'HttpPostedFileBase' so what can be my solutions ? – yogihosting May 13 '17 at 13:16
  • 1
    Suggest you look at [this answer](http://stackoverflow.com/questions/40199870/how-to-validate-file-type-of-httppostedfilebase-attribute-in-asp-net-mvc-4/40200034#40200034) for a validation attribute that works with `HttpPostedFileBase` (both client and server side validation) –  May 13 '17 at 13:17
  • 1
    @yogihosting Copy [this solution](http://stackoverflow.com/questions/31366243/fileextensions-attribute-of-dataannotations-not-working-in-mvc) – Mark C. May 13 '17 at 13:17
  • Thank you mark & stephen - i have understood. I think there is no inbuilt attribute provided by MVC to validate properties of HttpPostedFileBase type. So either make your own customattribute or change it to string. – yogihosting May 13 '17 at 13:24
  • You cant change it to a `string` (a file input binds to `HttpPostedFileBase`) –  May 13 '17 at 13:26

0 Answers0