Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to determine why ModelState.IsValid returns false?
Message
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Entity Framework
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01562997
Message ID:
01563072
Vues:
34
>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?

I added the following code to debug the problem:
try
            {
                if (ModelState.IsValid)
                {
                    Db.AddClient(model.Client);
                    return this.Client(collection);
                }
                else
                {
                    foreach (ModelState state in ViewData.ModelState.Values.Where(x => x.Errors.Count > 0))
                    {
                        var err = state;
                    }

                   // return new AjaxableViewResult("Edit", model);
                    return PartialView("_ClientForm", model);
                }

            }
            catch
            {
                return View("Edit",model);
            }
        }
So, I can see that at some point state.Error.Count = 1 and the Exception is "Client Name is required". However, I don't understand why this message is thrown at all as both Name property and Client.Name have correct value?

May be confusion is because I have external property the same as Client's name property?
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform