Validation of the repeated registration

Asked

Viewed 143 times

1

I have a class called Matricula. She gets a record of Aluno and of Serie.

I need to do a validation, where if the student is already enrolled in a registered series, the page presents a message stating that he is already enrolled and do not let him be enrolled again.

Below is the registration code:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace bj_cursosonline.Models
{
    public class Matricula
    {
        [Key]
        public int ID { get; set; }

        [Required(ErrorMessage = "Campo obrigatório!")]
        [Display(Name = "Aluno")]
        public int ALUNOSID { get; set; }

        [Required(ErrorMessage = "Campo obrigatório!")]
        [Display(Name = "Série")]
        public int SERIESID { get; set; }

        public virtual Series SERIES { get; set; }

        public virtual Alunos ALUNO { get; set; }
    }
}

Student Class:

public enum SEXOALUNO
{
    MASCULINO, FEMININO
}

public class Alunos
{
    [Key]

    public int ID { get; set; }

    [Display(Name = "Nome")]
    [Required(ErrorMessage = "Campo obrigatório!")]
    public string NOME { get; set; }

    [Display(Name = "Sexo")]
    [Required(ErrorMessage = "Campo obrigatório!")]
    public SEXOALUNO? SEXO { get; set; }

    [Required(ErrorMessage = "Campo obrigatório!")]
    [Display(Name = "Data de Nascimento")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
    [DataType(DataType.Date, ErrorMessage = "Data em formato inválido!")]
    public DateTime DATANASCIMENTO { get; set; }

    public virtual ICollection<Matricula>
        Matricula
    { get; set; }
}

Serial Class:

public enum TURMA
{
    A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z
}

public enum TURNO
{
    MANHÃ, TARDE, NOITE
}

public class Series
{
    [Key]
    public int ID { get; set; }

    [Required(ErrorMessage = "Campo Obrigatório!")]
    [StringLength(50)]
    [Display(Name = "Descrição:")]
    public string DESCRICAO { get; set; }

    [Display(Name = "Turma:")]
    public TURMA? TURMA { get; set; }

    [Display(Name = "Turno: ")]
    public TURNO? TURNO { get; set; }

    [Required(ErrorMessage = "Campo Obrigatório!")]
    [Display(Name = "Número da Sala:")]
    public int NUMSALA { get; set; }

    public virtual ICollection<Avaliacoes>
        Avaliacoes
    { get; set; }

    public virtual ICollection<Matricula>
        Matricula
    { get; set; }
}
  • Would you be able to put the SERIE and STUDENT class as well? In addition you are using what to carry out your queries in the Database?

  • 1

    The best way is by database. Via C# is much more code.

  • you could add a Unique index to your table, but check your business rule, for example: imagine that a student enrolled in the year 2015, did not complete the grade, and now in 2017 will enroll again. Would you have to delete the previous number plate? You’d lose his previous history ?

1 answer

1


In this case the ideal is before you save, you make a check if you already have such a registration in your Bank, if it returns an error message.

A good way to do this validation is by using Ivalidatableobject, with it you can implement your own validations in your class.

namespace bj_cursosonline.Models
{
    public class Matricula : IValidatableObject
    {
        [Key]
        public int ID { get; set; }

        [Required(ErrorMessage = "Campo obrigatório!")]
        [Display(Name = "Aluno")]
        public int ALUNOSID { get; set; }

        [Required(ErrorMessage = "Campo obrigatório!")]
        [Display(Name = "Série")]
        public int SERIESID { get; set; }

        public virtual Series SERIES { get; set; }

        public virtual Alunos ALUNO { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            // aqui você faz uma consulta no Banco de dados e busca se já existe um cadastro desse aluno na serie
            var result = db.Matriculas.Count(m => m.ALUNOSID == ALUNOSID && m.SERIESID == SERIESID); 
            if (result > 0)
            {
                yield return new ValidationResult("Já existe uma Matricula para esse aluno nessa Serie", new string[] { "SERIESID" });
            }

        }
    }
}

Here are more examples of how to use IValidatableObject /search?q=ivalidatableobject

Browser other questions tagged

You are not signed in. Login or sign up in order to post.