Listing on Asp.Net MVC

Asked

Viewed 359 times

1

Guys, I’m developing an application that manages enrolment courses, on my screen of listing the courses is like this:

inserir a descrição da imagem aqui

Only I need to make a new screen with another type of listing, and I would like the listing to be by course and show the students enrolled in this course. For example, the screen would have to be like this:

inserir a descrição da imagem aqui

Can someone help me?

Model Aluno

public class Aluno
{
    [Key]
    public int Id { get; set; }

    [Required(ErrorMessage = "O campo Nome é obrigatório")]
    [MinLength(4, ErrorMessage = "O campo Nome deve ter no mínimo 4 caracteres")]
    [Display(Name = "Nome")]
    public string Nome { get; set; }


    [Display(Name = "CPF")]
    [Required(ErrorMessage = "O campo CPF é obrigatório")]
    [Index(IsUnique = true)]
    [CPFAtributo]
    public string CPF { get; set; }

    [Display(Name = "RG")]
    public string RG { get; set; }

    [Display(Name = "E-mail")]
    [Required(ErrorMessage = "O campo E-mail é obrigatório")]
    [RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Endereço de e-mail informado não é válido.")]
    public string Email { get; set; }

    [Display(Name = "Usuário")]
    [MinLength(4, ErrorMessage = "O campo Usuário deve ter no mínimo 4 caracteres")]
    [Required(ErrorMessage = "O campo Usuário é obrigatório")]
    public string Usuario { get; set; }

    [Display(Name = "Senha")]
    [Required(ErrorMessage = "O campo Senha é obrigatório")]
    public string Senha { get; set; }


    [Display(Name = "Confirmar Senha")]
    [Compare("Senha", ErrorMessage = "As senhas não conferem")]
    public string ConfirmaSenha { get; set; }

    [Display(Name = "Telefone Celular")]
    [Required(ErrorMessage = "Informe um número de Celular")]
    public string Telefone_Celular { get; set; }

    [Display(Name = "Telefone Fixo")]
    public string Telefone_Fixo { get; set; }

    [Display(Name = "Endereço")]
    [Required(ErrorMessage = "O Endereço é obrigatório")]
    public string Endereco { get; set; }

    [Display(Name = "Estado")]
    [Required(ErrorMessage = "Informe um Estado")]
    public string Estado { get; set; }

    [Display(Name = "Complemento")]
    public string Complemento { get; set; }

    [Display(Name = "Matrícula")]
    [Required(ErrorMessage = "Informe a Matrícula")]
    public string Matricula { get; set; }

    [Display(Name = "Cargo Efetivo")]
    public string Cargo_Efetivo { get; set; }

    [Display(Name = "Cargo Comissionado")]
    public string Cargo_Comissionado { get; set; }

    [Display(Name = "Telefone Orgao")]
    public string Telefone_Orgao { get; set; }

    [Display(Name = "Nome Chefe Imediato")]
    public string Nome_Chefia_Imediato { get; set; }

    [Display(Name = "Telefone Chefe Imediato")]
    public string Telefone_Chefia_Imediato { get; set; }

    public string Perfil { get; set; }

    public ICollection<Curso> Cursos { get; set; }

Model Curso

public class Curso
{
    [Key]
    public int Id { get; set; }

    [Display(Name = "Curso")]
    [Required(ErrorMessage = "O campo Curso é obrigatório")]
    public string Nome_Curso { get; set; }

    [Display(Name = "Sigla")]
    public string Sigla { get; set; }

    [Display(Name = "Ementa")]
    [Required(ErrorMessage = "A Ementa é obrigatório")]
    public string Ementa { get; set; }

    [Display(Name = "Aprovado")]
    public bool Aprovado { get; set; }

    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    [DataType(DataType.Date)]
    [Display(Name = "Data de Início")]
    public DateTime Dt_Inicio { get; set; }

    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    [DataType(DataType.Date)]
    [Display(Name = "Data Final")]
    public DateTime Dt_Fim { get; set; }

    [Display(Name = "Turno")]
    [Required(ErrorMessage = "Informe um Turno")]
    public string Turno { get; set; }


    public string Status { get; set; }

    [Display(Name = "Quantidade de Vagas")]
    [Required(ErrorMessage = "A quantidade de Vagas é obrigatório")]
    public int Qtd_Vagas { get; set; }

    public int AlunoId { get; set; }

    public Aluno Aluno { get; set; }

Query code as first image above

public ActionResult MeusCursos()
    {
        var cursos = db.Cursos.Include(a => a.Aluno).ToList();
        return View(cursos);
    }
  • If possible, post the structure of your tables along with the code you are using to perform the query.

  • I edited the question @Randrade these are my tables.

1 answer

0


The modeling is incorrect. As it stands, a Curso belongs to only one Aluno.

The correct is to create an associative table. I suggest something like this:

public class AlunoCurso 
{
    [Key]
    public int Id { get; set; }
    public int AlunoId { get; set; }
    public int CursoId { get; set; }
    public bool Aprovado { get; set; }

    public virtual Aluno Aluno { get; set }
    public virtual Curso Curso { get; set; }
}

Change Aluno for:

public class Aluno 
{
    [Key]
    public int Id { get; set; }
    ...

    public virtual ICollection<AlunoCurso> AlunoCursos { get; set; }
}

And Curso for:

public class Curso
{
    [Key]
    public int Id { get; set; }
    ...

    public virtual ICollection<AlunoCurso> AlunoCursos { get; set; }
}

virtual is important for the Entity Framework to recognize that it is a browsing property (i.e., that the property is not persisted in bank, and only acts as a lazy load facilitator).

The Controller would look like this:

public ActionResult MeusCursos()
{
    var cursos = db.AlunoCursos
                     .Include(a => a.Aluno)
                     .Include(a => a.Curso)
                     .ToList();

    return View(cursos);
}
  • I need these two types of listing one I already have the other not, in this new scenario then I will have the two types of lists cited above @Gypsy? What about the use of virtual the relationship field between the tables would not appear in the database, is that it? For example, the property would not appear Alunoid inside the table Course

  • Hey @Gypsy I made the change based on your example only that the associative table Hallucinogenic, is not associating my Course or Student Id in it, it’s getting empty. Even if I register new courses and students. The fields Id, Alunoid, Cursoid and Approved are empty, it was not to associate as I was registering them?

  • @Beginner Yes, you will have the two types of lists only with the association. No, the relationship of the tables is done normally. The navigation property is only valid for the application code, not for the bank. The idea is that it makes development easier and easily enables you the lazy load feature.

  • @There’s no such thing as magic. In addition to registering students and courses, an additional step is needed to make the association between students and courses, something I believe does not yet exist in your code.

  • And how would I make this additional step to make that association between them @Gypsy?

Show 1 more comment

Browser other questions tagged

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