>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; } > } >}>
>var errors = ModelState.SelectMany(x => x.Value.Errors.Select(z => z.Exception));>
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?