1
I am on a project that I have a relationship of 1:N. Explaining better: Project is for a school where there are students and their occurrences. Then you would be a student for several occurrences:
Models Student.Cs
public class Aluno
{
    public long Id { get; set; }
    [Required(ErrorMessage = "O nome do aluno é obrigatório")]
    [MinLength(3, ErrorMessage = "O nome deve ter no mínimo 3 caracteres")]
    [Display(Name = "Nome do Aluno")]
    public string Nome { get; set; }
    [MinLength(3, ErrorMessage = "O nome deve ter no mínimo 3 caracteres")]
    [Display(Name = "Nome do Pai")]
    public string NomePai { get; set; }
   // [Required(ErrorMessage = "O nome da mãe é obrigatório")]
    [MinLength(3, ErrorMessage = "O nome deve ter no mínimo 3 caracteres")]
    [Display(Name = "Nome da Mãe")]
    public string NomeMae { get; set; }
    [MinLength(3, ErrorMessage = "O nome deve ter no mínimo 3 caracteres")]
    [Display(Name = "Nome do Responsável")]
    public string NomeResponsavel { get; set; }
   // [Required(ErrorMessage = "O endereço é obrigatório")]
    [MinLength(10, ErrorMessage = "O endereço deve ter no mínimo 10 caracteres")]
    [Display(Name = "Endereço")]
    public string Endereco { get; set; }
   // [Required(ErrorMessage = "A data de nascimento é obrigatória")]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    [DataType(DataType.Date)]
    [Display(Name = "Data de Nascimento")]
    public DateTime DataDeNascimento { get; set; }
    [MaxLength(4, ErrorMessage = "O ano letivo deve ter no máximo 4 caracteres: AAAA ")]
    [Display(Name = "Ano Letivo")]
    public string AnoLetivo { get; set; }
    [Required(ErrorMessage = "Informe o ano que o aluno está cursando")]
    public int Ano { get; set; }
    [Required(ErrorMessage = "Informe a turma do aluno")]
    public string Turma { get; set; }
   // [Required(ErrorMessage = "Informe o numero da turma")]
    public int Numero { get; set; }
    [Required(ErrorMessage = "informe o turno")]
    public string Turno { get; set; }
    //[Required(ErrorMessage="O telefone é obrigatório")]
    [Display(Name="Telefone")]
    public string Telefone { get; set; }
    [Display(Name="Telefone Alternativo")]
    public string TelefoneContato { get; set; }
    [Display(Name="Telefone Responsável")]
    public string TelefoneResponsavel { get; set; }
    [Display(Name = "Foto")]
    public string Foto { get; set; }
    public ICollection<Ocorrencia> Ocorrencias { get; set; }
    public Aluno()
    {
        this.Ocorrencias = new HashSet<Ocorrencia>();
    }
}
Models/Occurrences.Cs
public class Ocorrencia
{
    public long Id { get; set; }
    public string Tipo { get; set; }
    public string Causa { get; set; }
    public string Observacao { get; set; }
    public long AlunoId { get; set; }
    public virtual Aluno Aluno { get; set; }
}
I even made the relationship. Only that when generated the views of occurrences was generated a dropdownlist with the name of the students to be inserted the occurrence. But actually that’s not what I want.
And here the Controller:
 public ActionResult Index()
{
    var ocorrencias = db.Ocorrencias.Include(o => o.Aluno);
    return View(ocorrencias.ToList());
}
// GET: /Ocorrencias/Detalhes/5
public ActionResult Detalhes(long? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Ocorrencia ocorrencia = db.Ocorrencias.Find(id);
    if (ocorrencia == null)
    {
        return HttpNotFound();
    }
    return View(ocorrencia);
}
// GET: /Ocorrencias/Adiciona
public ActionResult Adiciona()
{
    ViewBag.AlunoId = new SelectList(db.Alunos, "Id", "Nome");
    return View();
}
// POST: /Ocorrencias/Adiciona
// To protect from overposting attacks, please enable the specific properties you            want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Adiciona([Bind(Include="Id,Tipo,Causa,Observacao,AlunoId")]    Ocorrencia ocorrencia)
{
    if (ModelState.IsValid)
    {
        db.Ocorrencias.Add(ocorrencia);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    ViewBag.AlunoId = new SelectList(db.Alunos, "Id", "Nome", ocorrencia.AlunoId);
    return View(ocorrencia);
}
// GET: /Ocorrencias/Edita/5
public ActionResult Edita(long? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Ocorrencia ocorrencia = db.Ocorrencias.Find(id);
    if (ocorrencia == null)
    {
        return HttpNotFound();
    }
    ViewBag.AlunoId = new SelectList(db.Alunos, "Id", "Nome", ocorrencia.AlunoId);
    return View(ocorrencia);
}
// POST: /Ocorrencias/Edita/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edita([Bind(Include="Id,Tipo,Causa,Observacao,AlunoId")] Ocorrencia ocorrencia)
{
    if (ModelState.IsValid)
    {
        db.Entry(ocorrencia).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    ViewBag.AlunoId = new SelectList(db.Alunos, "Id", "Nome", ocorrencia.AlunoId);
    return View(ocorrencia);
}
// GET: /Ocorrencias/Remove/5
public ActionResult Remove(long? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Ocorrencia ocorrencia = db.Ocorrencias.Find(id);
    if (ocorrencia == null)
    {
        return HttpNotFound();
    }
    return View(ocorrencia);
}
// POST: /Ocorrencias/Remove/5
[HttpPost, ActionName("Remove")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(long id)
{
    Ocorrencia ocorrencia = db.Ocorrencias.Find(id);
    db.Ocorrencias.Remove(ocorrencia);
    db.SaveChanges();
    return RedirectToAction("Index");
}
What I want is that the incident was tied to the student, I say his ID, and not a dropdownlist to choose the student. I want everything to stay in the same place. Someone has a tip?
It would be nice if you paste that Model, Controller and View scheme here, to make more sense.
– Leonel Sanches da Silva
What do I have in the other question ? Or all their code ?
– Érik Thiago
Let me do it. Better.
– Leonel Sanches da Silva
Quiet then !
– Érik Thiago