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

  • Home
  • SEARCH
  • 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 6872033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:52:38+00:00 2026-05-27T03:52:38+00:00

I have a weird situation where I have created a separate validation class for

  • 0

I have a weird situation where I have created a separate validation class for my Entity object:

   [MetadataType(typeof(TopTenFav_Validation))]
public partial class TopTenFav
{

}
public class TopTenFav_Validation
{

    [Required(ErrorMessage = "Youtube link is Required")]
    [StringLength(100, ErrorMessage="Youtube link cannot exceed 100 characters")]
    [Range(10,20)]
    public string YoutubeLink { get; set; }


    [StringLength(100, ErrorMessage = "Youtube link cannot exceed 50 characters")]
    [Required]
    [MinLength(5, ErrorMessage = "Youtube link cannot be shorter than 30 characters")]
    public string Title { get; set; }
}

The Entity object’s name is the same as my validation class – TopTenFav, so as I red it should automatically map the validation logic to my Entity Framework objects. I have a little form where I mapped textboxes to the model as follows:

         <div>
            @Html.TextBoxFor(m => m.topTenFav.YoutubeLink, new { id = "youTubeLinkTxt"          })
            @Html.ValidationMessageFor(m => m.topTenFav.YoutubeLink,"*")
        </div>

         <div>
            @Html.TextBoxFor(m => m.topTenFav.Title, new { id = "youTubeNameTxt" })
             @Html.ValidationMessageFor(m => m.topTenFav.Title,"*")
        </div>

The problem is that the required attribute is not working while others work, meaning that when I leave the textboxes empty and I make an ajax call to the server the code passes on the success but when I type in data that doesnt pass the minLenght validator the code goes to the error part of my ajax call as I expected. So what am I missing here that the required validator is not firing up when my textboxes are empty?

    $(document).on("click", ".btnAddTopTenFav", function () {
        var btnClicked = $(this);
        var txtLink = $('#youTubeLinkTxt').val();
        var txtName = $('#youTubeNameTxt').val();
        var subLink = txtLink.substr(31); //.replace(/.*\?v=/, '');
        var rank = $(this).parent().index() + 1;
        $("#hiddenRank").val(rank);

        $.ajax({
            beforeSend: function () { ShowAjaxLoader(); },
            url: "/Home/AddTopTenFav/",
            type: "POST",
            data: $("#AddTopTenFavForm").serialize(),
            success: function (data) { HideAjaxLoader(), ShowMsg("Song Added Successfully"), $(btnClicked).replaceWith('<a name="' + subLink + '" class="savedLinks"  href="#" >' + txtName + '</a><span name=' + data + ' class="btnDeleteTopTenFavSong dontDoAnything">x</span'); },
            error: function () { HideAjaxLoader(), ShowMsg("Song could not be added, please try again") }
        });


  [HttpPost]
    public ActionResult AddTopTenFav(HomeViewModel topTen)
    {
        if (ModelState.IsValid)
        {
            var top = new TopTenFav();
            top.Date = DateTime.Now;
            top.Rank = topTen.topTenFav.Rank;
            top.UserName = User.Identity.Name;
            top.YoutubeLink = topTen.topTenFav.YoutubeLink;
            top.Title = topTen.topTenFav.Title;
            repository.AddTopTen(top);
            repository.Save();
            return this.Content(top.SongId.ToString());
        }
        else 
        {
            return View();
        }
  • 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-05-27T03:52:39+00:00Added an answer on May 27, 2026 at 3:52 am

    The issue is that you are not invoking validation to occur before you are posting.

    The MinLength validators fire, because you are entering data into the field, then leaving it, which invokes the validation for that field. However, the Required validators will only fire if invoked by the form’s submit event, or if you manually invoke validation for the form.

    Add the following to wrap the code in your $(document).on("click", ".btnAddTopTenFav" function:

    $("form").validate().form();
    if ($("form").valid())
    {
        // insert the rest of your submission code here
    }
    

    This will invoke the validation, and will only submit if the form is valid.

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

Sidebar

Related Questions

I have this weird situation. I have these two classes: Public Class Entry End
I have a weird situation where the getter in a class returns a primitive
I have a weird situation where a modal popup gets hidden automatically when it
I have encountered a weird situation while updating/upgrading some legacy code. I have a
I have come across a situation (which I think is weird but is possibly
Hi this may seem like a weird question, but here's my situation: I have
I have a weird situation with AlarmManager. I am scheduling an event with AlarmManager
I have a really weird situation. The idea is there is a scroller that
Very weird situation going on with a FlowLayoutPanel... I have been dynamically adding user
I have a weird situation, client does't want their application to support multitasking so

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.