Estou desenvolvendo um projeto em Asp Net MVC 5 Utilizando do framework Entity, sou iniciante na linguagem e estou com dificuldades em trabalhar com listbox. No meu caso tenho uma View create de um determinado objeto, minha problemas em implementar outros dois objetos nessa mesma view.
São dois listbox, um recebe um objeto que consigo exibir e fazer a transferência via javascript para o outro listbox vazio, meu problema esta em grava este objeto do segundo listbox no banco! N gerando excessao no entity framework! Acredito que pode ser por conta do relacionamento entre os dois objetos erro gerado:
{“A instrução INSERT conflitou com a restrição do FOREIGN KEY “FK_dbo.ItemReuniaos_dbo.Grupoes_GrupoCod”. O conflito ocorreu no banco de dados “SGRvamtec”, tabela “dbo.Grupoes”, column ‘GrupoCod’.\r\nA instrução foi finalizada.”}
CODIGOS
objeto reunião:
namespace SGR_domain
{
public class Reuniao
{
[Key]
public int ReuniaoCod { get; set; }
[ForeignKey("Tema"),Display(Name = "Tema principal:")]
public int TemaCod { get; set; }
[Display(Name = "Dê um nome a reunião:"), Required(ErrorMessage = "Campo obrigatório:")]
public string ReuniaoDesc { get; set; }
[Display(Name = "Faça uma descrição para a reunião:")]
public string ReuniaoObs { get; set; }
public Tema Tema { get; set; }
public List<Grupo> Grupos { get; set; }
public List<ItemReuniao> ItemReuniaos { get; set; }
}
}
Objeto grupo:
namespace SGR_domain
{
public class Grupo
{
[Key]
public int GrupoCod { get; set; }
[Display(Name = "Nome do Grupo"), Required(ErrorMessage = "É obrigatorio colocar um nome ao grupo")]
public string GrupoNome { get; set; }
[Display(Name = "Descrição do grupo")]
public string GrupoDescricao { get; set; }
}
}
objeto itemReuniao:
namespace SGR_domain
{
public class ItemReuniao
{
[Key]
public int ItemCod { get; set; }
[ForeignKey("Reuniao")]
public int ReuniaoCod { get; set; }
[ForeignKey("Grupo")]
public int GrupoCod { get; set; }
public Reuniao Reuniao { get; set; }
public Grupo Grupo { get; set; }
}
}
Controler reunião:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "ReuniaoCod,TemaCod,ReuniaoDesc,ReuniaoObs,GrupoCod")] Reuniao reuniao, [Bind(Include = "ItemCod")] ItemReuniao itemReuniao)
{
if (ModelState.IsValid)
{
db.Reuniaos.Add(reuniao);
db.ItemReuniaos.Add(itemReuniao);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.GrupoCod = new SelectList(db.Grupoes, "GrupoCod", "GrupoNome", itemReuniao.GrupoCod);
ViewBag.ReuniaoCod = new SelectList(db.Reuniaos, "ReuniaoCod", "ReuniaoDesc", itemReuniao.ReuniaoCod);
ViewBag.TemaCod = new SelectList(db.Temas, "TemaCod", "TemaNome", reuniao.TemaCod);
return View(reuniao);
}
ViewCreate:
........Formulario da reunião(realiza criacao normalmente)........
<div class="container body-content" style="padding-left: 5%;">
<b>Grupos da reunião:</b>
<br /><br /><br />
<div class="form-group">
<br/>
<label>Selecione um Grupo:</label><br/>
<div class="col-md-1 " style="width: 20%; height: 100%">
@Html.ListBox("GrupoCod", null, htmlAttributes: new {@class = "form-control"})
</div>
<div class="col-md-2" >
<input type="button" value="Add" class="btn btn-default" onclick="AddItem();"/><br />
<input type="button" value="Remove" class="btn btn-default" onclick="RemoveItem();" />
<input type="button" value="Ad.Todos" class="btn btn-default" onclick="AddAll();" /><br />
<input type="button" value="R.Todos" class="btn btn-default" onclick="RemoveAll();" />
</div>
<div class="col-md-3 ">
@Html.ListBoxFor(model => model.ItemReuniaos, new List<SelectListItem>(), new {htmlAttributes = new {@class = "form-control"}})
</div>
<br/>
</div>
</div>
<div class="container body-content">
<br />
<br />
<div class="col-md-1" style="padding-left: 10%">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
<br />
}
</div>
@*Script de reunião*@
<script type="text/javascript">
function AddItem() {
$("#GrupoCod option:selected").appendTo("#ItemReuniaos");
$("#ItemReuniaos option").attr("selected", false);
}
function RemoveItem() {
$("#ItemReuniaos option:selected").appendTo("#GrupoCod");
$("#GrupoCod option").attr("selected", false);
}
function RemoveAll() {
$("#ItemReuniaos option").appendTo("#GrupoCod");
$("#GrupoCod").attr("selected", false);
}
function AddAll() {
$("#GrupoCod option").appendTo("#ItemReuniaos");
$("#ItemReuniaos").attr("selected", false);
}
</script>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}