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 8153601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:04:19+00:00 2026-06-06T16:04:19+00:00

Model: using System.ComponentModel.DataAnnotations; using MySite.Validators; namespace MySite.Models { public class AddItem { [Required(ErrorMessage =

  • 0

Model:

using System.ComponentModel.DataAnnotations;
using MySite.Validators;

namespace MySite.Models
{
    public class AddItem
    {
        [Required(ErrorMessage = "Name is required")]
        public string Name { get; set; }

        [TagValidation(ErrorMessage = "At least one tag is required")]
        public virtual List<int> Tags { get; set; }
    }
}

View:

@using (Html.BeginForm()) {
    ...
    <div class="editor-label">
        @Html.LabelFor(model => model.Tags, "Tags")
    </div>
    <div class="editor-field">
        @Html.ListBox("Tags")
        @Html.ValidationMessageFor(model => model.Tags)
    </div>
    ...
}

Validator:

using System.ComponentModel.DataAnnotations;

namespace MySite.Validators
{
    public class TagValidationAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            return false;
        }
    }
}

I want my validator to return false to begin with, just to make sure it’s working. However, if I don’t select any tags from the list and submit the form, it tries to process it without any errors indicating that I need to select a tag first.

What am I doing wrong here?

  • 1 1 Answer
  • 0 Views
  • 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-06-06T16:04:20+00:00Added an answer on June 6, 2026 at 4:04 pm

    I had commented out the if (ModelState.IsValid == false) check in my controller, so I wasn’t getting any validation. The reason I did this, initially, was because I was getting an error when I tried to pass the model back to the view, because the ListBox field in the view expected a IEnumerable and not a List.

    Here’s how I fixed both problems (in the controller):

    [HttpPost]
    public ActionResult AddItem(AddItem AddItem)
    {
        if (ModelState.IsValid == false)
        {
            ModelState.AddModelError("", "Model not valid.");
    
            List<Tag> Tags = Db.Tags.ToList();
    
            ViewBag.Tags = new SelectList(Tags, "TagId", "Name");
    
            return View(AddItem);
        }
    
        //...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations; namespace yplaylist.Models { [MatadataType(typeof(Song_Validation))]
I have the following code block: - using System.ComponentModel.DataAnnotations; public class NewsItem { [RegularExpression(System.Configuration.ConfigurationSettings.AppSettings[UrlRegEx],
Good day! I've the following ViewModel class I use for login form: using System.ComponentModel.DataAnnotations;
I have a model class similar to the following: using System; using System.Collections.Generic; using
I'm trying to create a UniqueAttribute using the System.ComponentModel.DataAnnotations.ValidationAttribute I want this to be
I'm using System.ComponontModel.DataAnnotations to validate my model objects. How could I replace messages standard
My domain model is using System.Net.Uri to represent URLs, and System.Drawing.Color to represent colors.
This is my model of my asp.net mvc 2 project : using System; using
What are the best practices for using use cases to model system behavior that
I've been looking for hours the solution. I've 3 models atm. Priority: using System;

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.