Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to determine why ModelState.IsValid returns false?
Message
 
 
To
All
General information
Forum:
ASP.NET
Category:
Entity Framework
Title:
How to determine why ModelState.IsValid returns false?
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01562997
Message ID:
01562997
Views:
57
Hi everybody,

I have the following ViewModel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CardNumbers.Objects;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

using DataAnnotationsExtensions;
using System.ComponentModel.DataAnnotations.Schema;

namespace CardNumbers.Models
{
    public class ClientViewModel
    {
        public Client Client { get; set; }

        [Key]
        [Editable(false)]
        [Column("ClientId", TypeName = "int")]
        public virtual int? ClientId
        {
            get
            {
                if (Client == null)
                    return null;
                else
                    return Client.Id;
            }

            set { Client.Id = value ?? 0; }
        }
        [Required]
        [DisplayName("Client No")]
        [UIHint("Number")]
        [Column("client_no", TypeName = "smallint")]
        [Remote("doesClientNoExist", "Client", HttpMethod = "POST",
            AdditionalFields = "ClientId",
            ErrorMessage = "Client Number already exists. Please enter a different Client Number.")]
        public virtual Int16 Number
        {
            get
            {
                if (Client == null)
                    return 0;
                else
                    return Client.Number;
            }

            set { Client.Number = value; }
        }

        [Required]
        [Column("client_name", TypeName = "varchar")]
        [DisplayName("Client Name")]
        [MaxLength(30, ErrorMessage = "Client Name should not be longer than 30 characters")]
        [MinLength(3, ErrorMessage = "Client Name is too short")]
        [Remote("doesClientNameExist", "Client", HttpMethod = "POST", AdditionalFields = "ClientId",
            ErrorMessage = "Client Name already exists. Please enter a different Client Name.")]
        public virtual string Name
        {
            get
            {
                if (Client == null)
                    return "";
                else 
                    return Client.Name;            
            }

            set { Client.Name = value; }
        }

        public int id { get; set; }
    }
}
In the form I enter both required fields (Name and Client No) and go to save method. But the ModelState.IsValid returns false while error count is 0. How can I figure out why it is returning false?

Do you see what may be wrong with my implementation here? I am trying to expose Name and Number properties because I need them somehow for RemoteValidator and I can not make them work if I use them through Model.Clients. properties that's why I attempted to expose these two properties the way I did.

I added this
var errors = ModelState.SelectMany(x => x.Value.Errors.Select(z => z.Exception));
it it returned Count = 1
[0] null

which does not help me.

How can I know what is wrong with the model?
If it's not broken, fix it until it is.


My Blog
Next
Reply
Map
View

Click here to load this message in the networking platform