How to save Dysicpline?

Asked

Viewed 68 times

3

I have the following problem with my application. The course table is related to the Discipline table, that is, 1 Discipline belongs to several courses, and at the time of registering a course and adding more disciplines to this course I can not perform this action. Screen print: inserir a descrição da imagem aqui
inserir a descrição da imagem aqui

[Controller]
public ActionResult AddDisciplina(int id, Curso curso) { 
    @ViewBag.id = curso.CursoID=id; 
    ViewBag.d = new SelectList(db.disciplina, "DisciplinaID", "nome"); 
    return View(); 
}

[HttpPost, ActionName("AddDisciplina")]
[ValidateAntiForgeryToken]
public ActionResult AddDisciplina([Bind(Include = "CursoID,DisciplinaID")]Curso curso)
{
    return View();
}

[View]
<h2>AddDisciplina</h2> 
<div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
         <input type="hidden" id=" cursoID" name="cursoID" value="@ViewBag.id"> 
         @Html.DropDownList("d", null, htmlAttributes: new { @class = "form-control" }) 
     </div> 
</div> 
<br/> 
<div class="form-group"> 
     <div class="col-md-offset-2 col-md-10"> 
          <input type="submit" value="Create" class="btn btn-default" /> 
     </div> 
</div>
  • You can put in your question the code that performs this insertion?

  • Where is the Action that the data persists? Tip: Also calls AddDiscipina, but it has a [HttpPost] on top.

1 answer

3

There’s nothing in your Action to save. Obviously it won’t work.

It has to be something like this:

[HttpPost, ActionName("AddDisciplina")]
[ValidateAntiForgeryToken]
public ActionResult AddDisciplina([Bind(Include = "CursoID,DisciplinaID")]Curso curso)
{
    if (ModelState.IsValid)
    {
        var disciplina = db.Disciplinas.SingleOrDefault(d => d.DisciplinaID == DisciplinaID);
        var curso = new Curso {
            Disciplina = disciplina
        };

        context.Cursos.Add(curso);
        context.SaveChanges();
    }

    return View();
}

Browser other questions tagged

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