ActionResult Create error

0

I have a exception , in the method of creating an object with a foreign key, but it points the error in my db.SaveChanges() with the following message:

  

DbUpdateException was unhandled by user code.

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "CursoId,NomeCurso,Creditos,UniversidadeId")] Curso curso)
    {
        if (ModelState.IsValid)
        {
            db.Cursos.Add(curso);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        ViewBag.UniversidadeId = new SelectList(db.Universidades, "UniversidadeId", "NomeUniversidade", curso.UniversidadeId);
        return View(curso);
    }    

My view Create:

@model PortalEscola.WEB.Models.Curso

@{
ViewBag.Title = "Create";
}



@using (Html.BeginForm()) 
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Criar curso</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.NomeCurso, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.NomeCurso, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.NomeCurso, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Creditos, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Creditos, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Creditos, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.UniversidadeId, "UniversidadeId", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("UniversidadeId", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.UniversidadeId, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Salvar" class="btn btn-default" />
        </div>
    </div>
</div>
}

<div>
        @Html.ActionLink("Voltar", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

And my class Travel is like this:

public class Curso
{

    public int CursoId { get; set; }

    [Display(Name = "Nome do Curso")]
    public string NomeCurso { get; set; }
    public int Creditos { get; set; }
    public int UniversidadeId { get; set; }

    [ForeignKey("UniversidadeId")]
    [Display(Name = "Universidade")]
    public virtual Universidade Universidade { get; set; }
    public virtual ICollection<Inscricao> Inscricoes { get; set; }

}

I can save changes to university, which pull DropDownList with a University. but when I register the Course, it gives me error in db.SaveChanges in the ActionResult Create method, to register the course.

    
asked by anonymous 12.03.2018 / 20:07

0 answers