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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:42:22+00:00 2026-05-12T13:42:22+00:00

I am using the Data Annotation Validator, outlined here: http://www.asp.net/learn/mvc/tutorial-39-cs.aspx The Data Annotations Model

  • 0

I am using the Data Annotation Validator, outlined here:

http://www.asp.net/learn/mvc/tutorial-39-cs.aspx

The Data Annotations Model Binder is on codeplex:

http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471

I’ve downloaded the code, built it, and referenced the new System.ComponentModel.DataAnnotations.dll. I’ve also set the default model binder to use Microsoft.Web.Mvc.DataAnnotations.dll. It works fine when binding to a simple object (e.g, Order), but if I bind to an Order that has a CreditCard object, I get an error in the new binder:

Object reference not set to an instance of an object, DataAnnotationsModelBinder.cs Line: 60.

In my example, the fullPropertyKey is “Card”, and modelState is null, so its obviously having an issue with the Card property of Order.

ModelState modelState = bindingContext.ModelState[fullPropertyKey];

// Only validate and bind if the property itself has no errors
if (modelState.Errors.Count == 0) {
    if (OnPropertyValidating(controllerContext, bindingContext, propertyDescriptor, newPropertyValue)) {
           SetProperty(controllerContext, bindingContext, propertyDescriptor, newPropertyValue);
           OnPropertyValidated(controllerContext, bindingContext, propertyDescriptor, newPropertyValue);
    }
}

Does the data annotaions binder support this? The stock binder has no issue with the Order -> CreditCard structure (minus the validation, of course). Test code:

Controller & Model:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.ComponentModel.DataAnnotations;

namespace PAW.Controllers
{
    //MODEL
    public class Order
    {
        [Required]
        [DataType(DataType.Text)]
        [StringLength(5)]
        public string CustomerName { get; set; }

        public CreditCard Card { get; set; }

        public Order()
        {
            this.Card = new CreditCard();
        }
    }
    public class CreditCard
    {
        [StringLength(16), Required]
        public string Number { get; set; }
    }

    //CONTROLLER
    public class TestController : Controller
    {
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Index()
        {
            return View(new Order());
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(Order o)
        {
            if (ModelState.IsValid)
            {
                //update
            }
            return View(o);
        }
    }
}

The View:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<PAW.Controllers.Order>" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<body>        
    <% using(Html.BeginForm()) { %>

        <%=Html.ValidationSummary("Errors: ") %>

        <div>
            Name (max 5 chars): <%=Html.TextBox("CustomerName", Model.CustomerName)%>
        </div>

        <div>
            CC#: <%=Html.TextBox("Card.Number", Model.Card.Number)%>
        </div>

        <input type="submit" value="submit" />
    <% } %>
</body>
</html>
  • 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-12T13:42:22+00:00Added an answer on May 12, 2026 at 1:42 pm

    This is infact a bug in the DataAnnotationModelBinder. It’s suppose to be fixed in the upcoming version though.

    In this similar question on SO i posted my quick fix in the DataAnnotationModelBinder to make it work and a quick example of complex/deep binding.

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

Sidebar

Ask A Question

Stats

  • Questions 227k
  • Answers 227k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Instead of a static control, use a RichEdit control containing… May 13, 2026 at 1:26 am
  • Editorial Team
    Editorial Team added an answer Since the introduction of ES6, you can use the spread… May 13, 2026 at 1:26 am
  • Editorial Team
    Editorial Team added an answer You can declare them in the header with the protocol… May 13, 2026 at 1:25 am

Related Questions

I am working on asp.net mvc application using the mvc2 framework. Here is the
What does the castle validators offer me over standard data annotations? I am a
I am a newbie in Hibernate. I am working on a cloud service data
I'm just getting started on a new MVC project, and like a good boy

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.