Error registering data in ViewModel

2

I have this question where I where according to the answers, my logic ... Only now, when registering the data, an error was generated. This error refers to the property of a view that can not be null, which is the ID. That is, from what I understand, the data was not registered, and for some reason this occurred not generating any record in the table, or is not recognizing the logic ...

Well, my controller looks like this:

 // 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);
    }

Is there something wrong with this?

EDIT

The corresponding view:

@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>

}

Table2

@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>

CLiCent

@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>

Table3

@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>

Table4

@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>

Table5

@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>
    
asked by anonymous 28.05.2016 / 23:00

1 answer

1

There are some problems there. Since all relations are from N to 1 (and not from 1 to N), Partials will have to be called like this:

@Html.Partial("_PartialCliente", Model)
@Html.Partial("_PartialTabela2", Model)
@Html.Partial("_PartialTabela3", Model)
@Html.Partial("_PartialTabela4", Model)
@Html.Partial("_PartialTabela5", Model)

This is because, when creating form fields, Helper does not specify which entity the field comes from. For example:

<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>

It will create:

<div class="form-group">
    <label class="control-label col-md-2" for="AnaRotina">Rotina</label>
    <div class="col-md-10">
        <input type="text" name="AnaRotina" id="AnaRotina" class="form-control" />
        <span ... />
    </div>
</div>

The right thing would be:

<div class="form-group">
    <label class="control-label col-md-2" for="Tabela2.AnaRotina">Rotina</label>
    <div class="col-md-10">
        <input type="text" name="Tabela2.AnaRotina" id="Tabela2_AnaRotina" class="form-control" />
        <span ... />
    </div>
</div>

Then we have to change @model of all Partials to:

@model MeuProjeto.Models.AnamineseViewModel

And the fields:

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

Another thing is that you are creating @Html.BeginForm in all Partials . This is wrong. @Html.BeginForm is in the View parent (which is already done correctly). Delete the @Html.BeginForm of Partials .

    
28.05.2016 / 23:29