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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:27:42+00:00 2026-05-14T00:27:42+00:00

Going mad now. I have a MVC solution that i’ve upgraded from MVC 1

  • 0

Going mad now. I have a MVC solution that i’ve upgraded from MVC 1 to 2. It all works fine…. except the Validation!

Here’s some code:

In the controller:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.UI;
using MF.Services.Authentication;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace MF.Controllers
{
     //basic viewmodel
     public class LogOnViewData
     {
     [Required]
     public string UserName { get; set; }

     [Required]
     public string Password { get; set; }
     }

    [HandleError]
public class AccountController : Controller
{
         [HttpPost]
         public ActionResult LogOn(LogOnViewData lvd, string returnUrl)
         {

         if (ModelState.IsValid)
         {
        //do stuff - IsValid is always true
         }
         }
    }
}

The ModelState is always valid. The model is being populated correctly however. Therefore, if I leave both username and password blank, and post the form the model state is still valid. Argh!

Extra info: using structure map for IoD. Previously, before upgrading to MVC 2 was using the MS data annotation library so had this in my global.asax.cs:
ModelBinders.Binders.DefaultBinder = new Microsoft.Web.Mvc.DataAnnotations.DataAnnotationsModelBinder();

Have removed that now.

I’m sure i’m doing something really basic and wrong. If someone could point it out that would be marvellous.
Cheers

  • 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-14T00:27:42+00:00Added an answer on May 14, 2026 at 12:27 am

    Half way through the development of MVC2, they went from input validation to model validation, which should in all cases validate your object completely. Make sure you’re using the latest version (RTM).

    However, [Required] merely indicates the attribute must not be null. Unfortunately, String.Empty -which is the default for strings- is not null, so model validation will pass for empty strings.

    See this post by Brad Wilson for important details.

    As a solution, you could use the [RegularExpression("....")] to impose restrictions on the minimum string length and allowed characters.

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

Sidebar

Related Questions

Could I get some confirmations from the community that I'm not going mad, and
this is driving me MAD now.. I have a UItableview. Based on an NSMutableArray,
At first I thought I was going mad, but now I know I'm not.
I'm going mad using navigation controllers on the iPhone. I have an app, with
I'm going mad with, probably, a stupid problem. I have 3 strings: year, month
While going through university and from following the development of SO, I've heard a
Am I missing something fundamental to the principals of MVC or am I going
I maybe going mad, but for the life of me I can't find the
I have honestly tried several different avenues from google on this erro but I
I'm slowly going mad over the following problem: In my ASP.NET page (target: .NET

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.