MVC上的实体框架验证

时间:2012-07-23 09:04:35

标签: asp.net-mvc entity-framework

我有一个asp.net mvc项目,我用过ef。 它的“添加”控制器=>

    [Authorize(Roles = "Admin")]
    public ActionResult Add()
    {
        using (Process _process = new Process())
            ViewBag.KlinikListesi = _process.KlinikleriGetir();
        return View();
    }

    [Authorize(Roles = "Admin")]
    [HttpPost]
    public ActionResult Add(uzmanlik_egitim _ueModel)
    {
        using (Process _process = new Process())
        {

            ViewBag.KlinikListesi = _process.KlinikleriGetir();
            if (Request.QueryString["userName"] != null)
            {
                _ueModel.kullanici_adi = Request.QueryString["userName"].ToString();
                _process.Add(_ueModel);
            }
        }
        return View();
    }

我创建了View(选择的模型为ef表(uzmanlik_egitim))

效果很好。

但我需要验证字段,我试过=>

     //[MetadataType(typeof(uzmanlik_egitim_metadata))]
//public partial class uzmanlik_egitim
//{

//}

//public class uzmanlik_egitim_metadata
//{
//    [Required]
//    public string kullanici_adi { get; set; }

//    [Required]
//    public int ID { get; set; }

//    [Required]
//    public string klinik { get; set; }

//    [StringLength(1, ErrorMessage = "En Fazla 1 Karakter")]
//    public int? ulusal_kongre { get; set; }

//    [StringLength(1, ErrorMessage = "En Fazla 1 Karakter")]
//    public int? uluslararasi_kongre { get; set; }

//    [StringLength(1, ErrorMessage = "En Fazla 1 Karakter")]
//    public int? yurtici_sunum { get; set; }

//    [StringLength(1, ErrorMessage = "En Fazla 1 Karakter")]
//    public int? yurtdisi_sunum { get; set; }

//    [StringLength(1, ErrorMessage = "En Fazla 1 Karakter")]
//    public int? yurtici_bilimsel_yayin { get; set; }

//    [StringLength(1, ErrorMessage = "En Fazla 1 Karakter")]
//    public int? yurtdisi_bilimsel_yayin { get; set; }
//}

但由于类型的原因我收到了错误 我被困了。

1 个答案:

答案 0 :(得分:1)

我认为RangeAttribute更合适,因为您使用int作为属性类型。

[Range(0, 9, ErrorMessage = "En Fazla 1 Karakter")]
public int? ulusal_kongre { get; set; }