blackraist
blackraist

Reputation: 183

Entity Framework Validation on MVC

i've an asp.net mvc project that i used ef. its "Add" controller =>

    [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();
    }

And i created View (selected model as ef table(uzmanlik_egitim))

it works perfectly.

But i need to validate fields, i tried =>

     //[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; }
//}

but i'm getting error because of type i'm stucked.

Upvotes: 1

Views: 156

Answers (1)

Peter Porfy
Peter Porfy

Reputation: 9030

I think RangeAttribute is more appropriate here because you are using int as property type.

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

Upvotes: 1

Related Questions