Error logging data in Viewmodel

Asked

Viewed 58 times

2

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

Well, my controller is that way:

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

There’s something wrong with that?

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>

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>

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>
  • What error appears in?

  • An Exception of type 'System.Argumentnullexception' occurred in Entityframework.dll but was not handled in user code Additional information: Value cannot be null. In db.CliCliente.Add (anaminese.Client)

  • Post on your question how are yours Views, please.

  • As Partials also.

  • They’re pretty big... Something’s wrong?

  • No. No problem at all.

  • I put the partials

  • @John understood yes... Only that in the fields of Id I do not need you to be editorfor... The editorfor will create a text field, this can confuse the user and induce him to fill in this field, since it is not to fill in.... And I tried like the view with my Viewmodel, only they can’t access it... But I’ll try here to see!

Show 3 more comments

1 answer

1


There are some problems there. As all relations are from N to 1 (and not from 1 to N), the 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>

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>

So we have to change @model of all the Partials for:

@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’re creating @Html.BeginForm in all Partials. This is wrong. @Html.BeginForm stays in the View main (which is already done correctly). Delete the @Html.BeginForm of Partials.

  • But I tried that way, only the View can’t change and access that way... but I’ll try here then!

  • Another thing, regarding the ID fields, are they necessary? That is, when I created scaffolding, dropdown-type fields were created and referring to Ids... They are needed in views?

  • Dude worked out! But now I’m having another problem.. While saving in the bank, some relationships are not being respected and saved in the bank.... I even made that question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.