0

我正在使用这个 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; }
    }
}

我必须通过模型的公共属性公开两个客户端属性的原因是我正在尝试使用 RemoteValidator。无论如何,我输入了必填字段名称和编号,然后尝试保存模型,它返回 ModelState.IsValid false。我怎样才能看到有什么问题?我正在尝试使用 jquery 模态对话框,因此重新加载不是很简单。

提前致谢。

4

0 回答 0