How to save Disicpline?

3

I have the following problem in my application.The course table is related to the Discipline table, ie 1 Discipline belongs to several course, and at the time of registering a course and adding more courses to this course I can not accomplish this action. Print the screens:

[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>
    
asked by anonymous 22.09.2014 / 22:22

1 answer

3

There's nothing in your Action to save. Obviously it will not 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();
}
    
22.09.2014 / 23:01