Tenho essa pergunta onde onde fiz de acordo com as repostas, minha lógica... Só que agora, ao cadastrar os dados, um erro foi gerado. Esse erro se refere a propriedade de uma view que não pode ser nula, que é o ID. Ou seja, pelo que entendi, os dados não foram cadastrados, e por algum motivo isso ocorreu não gerando nenhum registro na tabela, ou não está reconhecendo a lógica...
Bem, o meu controller esta dessa forma:
// GET: Controller/Create
public ActionResult Create()
{
return View(new AnamineseViewModel
{
CliCliente = new CliCliente(),
Tabela2= new Tabela2(),
Tabela3 = new Tabela3(),
Tabela4 = new Tabela4(),
Tabela5 = new Tabela5(),
});
}
// POST: Controller/Create
[HttpPost]
public ActionResult Create(AnamineseViewModel anaminese)
{
// TODO: Add insert logic here
if (ModelState.IsValid)
{
db.CliCliente.Add(anaminese.CliCliente);
db.Tabela2.Add(anaminese.Tabela2);
db.Tabela3.Add(anaminese.Tabela3);
db.Tabela4.Add(anaminese.Tabela4);
db.Tabela5.Add(anaminese.Tabela5);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(anaminese);
}
Tem alguma coisa errada com isso?
EDIT
A view correspondente:
@model MeuProjeto.Models.AnamineseViewModel
@{
ViewBag.Title = "Create";
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<h2>Create</h2>
@Html.Partial("_PartialCliente", Model.CliCliente)
@Html.Partial("_PartialTabela2", Model.Tabela2)
@Html.Partial("_PartialTabela3", Model.Tabela3)
@Html.Partial("_PartialTabela4", Model.Tabela4)
@Html.Partial("_PartialTabela5", Model.Tabela5)
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
}
Tabela2
@model NutriSport.Models.AnaAnamineseAlimentar
@*@using (Html.BeginForm())
{
@Html.AntiForgeryToken()*@
<div class="form-horizontal">
<h4>AnaAnamineseAlimentar</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.AnaObjetivosMetas, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AnaObjetivosMetas, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AnaObjetivosMetas, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AnaRotina, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AnaRotina, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AnaRotina, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AnaDisposicao, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.AnaDisposicao)
@Html.ValidationMessageFor(model => model.AnaDisposicao, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AnaComorbidades, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AnaComorbidades, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AnaComorbidades, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AnaResfriado, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.AnaResfriado)
@Html.ValidationMessageFor(model => model.AnaResfriado, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AnaMedicamentos, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AnaMedicamentos, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AnaMedicamentos, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AnaConsumoAgua, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.AnaConsumoAgua, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AnaConsumoAgua, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CliId, "CliId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Hidden("CliId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.CliId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RecId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.HiddenFor(model => model.RecId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RecId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RefId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.HiddenFor(model => model.RefId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RefId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.QfaId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.HiddenFor(model => model.QfaId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.QfaId, "", new { @class = "text-danger" })
</div>
</div>
@*<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>*@
</div>
@* }*@
<div>
@Html.ActionLink("Back to List", "Index")
</div>
CliCLiente
@model NutriSport.Models.CliCliente
@*@using (Html.BeginForm())
{
@Html.AntiForgeryToken()*@
<div class="form-horizontal">
<h4>CliCliente</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.CliNome, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CliNome, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CliNome, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CliDataNascimento, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CliDataNascimento, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CliDataNascimento, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CliEmail, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CliEmail, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CliEmail, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CliSexo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CliSexo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CliSexo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CliCidade, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CliCidade, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CliCidade, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CliTelefone, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CliTelefone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CliTelefone, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CliCelular, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CliCelular, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CliCelular, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CliOcupacao, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CliOcupacao, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CliOcupacao, "", new { @class = "text-danger" })
</div>
</div>
@*<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>*@
</div>
@* }*@
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Tabela3
@model NutriSport.Models.RecRecordatorio
@*@using (Html.BeginForm())
{
@Html.AntiForgeryToken()*@
<div class="form-horizontal">
<h4>RecRecordatorio</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.RecId, "RecId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Hidden("RecId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.RecId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RecHoraDorme, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RecHoraDorme, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RecHoraDorme, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RecHoraAcorda, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RecHoraAcorda, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RecHoraAcorda, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AnaId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.HiddenFor(model => model.AnaId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AnaId, "", new { @class = "text-danger" })
</div>
</div>
@*<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>*@
</div>
@* }*@
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Tabela4
@model NutriSport.Models.RefRefeicao
@*@using (Html.BeginForm())
{
@Html.AntiForgeryToken()*@
<div class="form-horizontal">
<h4>RefRefeicao</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.RefId, "RefId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Hidden("RefId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.RefId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RefTipo, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RefTipo, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RefTipo, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RefHorarioLocal, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RefHorarioLocal, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RefHorarioLocal, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.RefAlimentosQuantidades, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.RefAlimentosQuantidades, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.RefAlimentosQuantidades, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AnaId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.HiddenFor(model => model.AnaId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AnaId, "", new { @class = "text-danger" })
</div>
</div>
@*<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>*@
</div>
@* }*@
<div>
@Html.ActionLink("Back to List", "Index")
</div>
Tabela5
@model NutriSport.Models.QfaQuestionarioFrequenciaAlimentar
@*@using (Html.BeginForm())
{
@Html.AntiForgeryToken()*@
<div class="form-horizontal">
<h4>QfaQuestionarioFrequenciaAlimentar</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.QfaId, "QfaId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.Hidden("QfaId", null, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.QfaId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.QfaGrupoAlimentar, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.QfaGrupoAlimentar, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.QfaGrupoAlimentar, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.QfaDia, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.QfaDia, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.QfaDia, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.QfaSemana, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.QfaSemana, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.QfaSemana, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.QfaMes, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.QfaMes, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.QfaMes, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.QfaNunca, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.QfaNunca)
@Html.ValidationMessageFor(model => model.QfaNunca, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.QfaRaramente, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.QfaRaramente)
@Html.ValidationMessageFor(model => model.QfaRaramente, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.QfaObservacao, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.QfaObservacao, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.QfaObservacao, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.AnaId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.HiddenFor(model => model.AnaId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.AnaId, "", new { @class = "text-danger" })
</div>
</div>
@*<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>*@
</div>
@* }*@
<div>
@Html.ActionLink("Back to List", "Index")
</div>