Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3634546
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:47:33+00:00 2026-05-19T00:47:33+00:00

I have 2 scenarios that I need some help with re validation in my

  • 0

I have 2 scenarios that I need some help with re validation in my ASP.NET MVC application. I’m aware that having validation within the controller is not ideal, so am looking to keep this elsewhere – perhaps with my models where I can.

1) I have a model with various properties, some of which have validation against them using DataAnnotations. I’m then using the Html helper methods within my view to expose any validation errors against the relevant fields. For the most part, these work as expected. The exception I’ve come up against is where one of the fields in my view is a dropdown list. The first item within my list is empty/blank, the rest are genuine values. The property in my model that this field relates to has the following against it:

[Required(ErrorMessage = "A value from the list is required")]

At present, if I leave the default value in the list (blank) and don’t select a genuine value from the list, I want it to render the validation error message, but it’s currently treating it as if it were a valid value, and passing that validation.

How can I get it to fail validation if that blank/empty list item is submitted?

2) On one of my views, I have a few file upload controls, enabling the user to upload images to the website. These fields are not directly bound to any properties within my model – only the resulting filename’s (once the file has been uploaded, converted, renamed etc.) are then assigned to ‘Filename1’, ‘Filename2’ etc. properties within my model.

So, I am wondering how to best go about validating that these mandatory file uploads? At present I am doing the following for each of the file upload controls, within my controller(!):

HttpPostedFileBase file = null;

file = Request.Files["Filename1"];
        if (file != null && file.ContentLength == 0)
                ModelState.AddModelError("Filename1", "Image1 is required");

Once this is done for each of the file upload controls, I check if the ModelState is valid:

if (ModelState.IsValid)

I’m sure there must be a better way of performing this validation, and I’d imagine it’s not ideal to have this in the controller, but I’m not sure the best way to handle this.

I’d appreciate any help with these 2 scenarios.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T00:47:34+00:00Added an answer on May 19, 2026 at 12:47 am

    Here is my code (explanations later) :

    The form:

    <% using (Html.BeginForm("TestForm", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
       { %>
       <%: Html.DropDownList("ComboboxValue", new SelectList(Model.ComboboxValues)) %><br />
       <input type="file" id="FileUpload" name="FileUpload" /><br />
       <input type="submit" id="submit" name="submit" value="Valider" />
    <%} %>
    

    The model:

    public class TestFormModel
    {
        [Required(ErrorMessage = "A value from the list is required")]
        public string ComboboxValue { get; set; }
    
        public List<string> ComboboxValues { get; set; }
    
        public HttpPostedFileBase FileUpload { get; set; }
    
        public ModelStateDictionary IsFileValid()
        {
            ModelStateDictionary modelState = new ModelStateDictionary();
            modelState.AddModelError("FileUpload", "Here is the problem.");
            return modelState;
        }
    

    The controller :

        public ActionResult TestForm()
        {
            TestFormModel model = new TestFormModel();
            model.ComboboxValues = new List<string>(){
                "", "Red", "Blue", "Yellow"
            };
            return View("TestForm", model);
        }
        [HttpPost]
        public ActionResult TestForm(TestFormModel model)
        {
            model.ComboboxValues = new List<string>(){
                "", "Red", "Blue", "Yellow"
            };
            ModelState.Merge(model.IsFileValid());
            return View("TestForm", model);
        }
    

    1) If you have a null value, your model wont be valid.

    2) To avoid Request.Files[“Filename1”]; you can “type” your form (new { enctype = “multipart/form-data” }).
    With this, your model will contain the file.
    You can add an extension method, for example :

    public static bool IsCSVValid(this HttpPostedFileBase file)
    {
        return (file != null && file.ContentLength != 0 && file.FileName.EndsWith(".CSV", StringComparison.InvariantCultureIgnoreCase));
    }
    

    And you can add error from somewhere with ModelState.Merge().

    I dont know if it’s a “good thing to do”, but it works pretty well 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I need some help understanding how static objects persist in an ASP.Net
i need some help with c# and nhibernate. I'm working on a project that
I'm building a Flex application that will need run under two different deployment scenarios:
I need some help :) I have a tab activity with four sub activities.
I have some what complex UI requirement, need help in understanding type of control/style/template
I need some help to store some data efficiently. I have a large list
I have a client/server application written in C#/.NET 3.5 that I want to do
I need help with a question that I could not answer yet. I have
I have a scenario that, I am creating dynamic html content and I need
We have 18 databases that should have identical schemas, but don't. In certain scenarios,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.