Edit MVC . NET error

Asked

Viewed 166 times

-7

I have an error when I edit my registration, my Physical Model is like PartialView,the following error occurs:

Attaching an entity of type 'Projeto.Models.Fisica' failed because another entity of the same type already has the same primary key value

MODEL REGISTER

public class Cadastro
{
    public Pessoa Pessoa { get; set; } 
    public Fisica Fisica { get; set; }
}

PHYSICAL MODEL

    public class Fisica : Pessoa
    {
        public string RG { get; set; }
    }

MODEL PERSON

    public partial class Pessoa
    {
      [Key]
      public int IdPessoa { get; set; }
      public string Nome { get; set; }
    }

VIEW EDIT

    @model CodeFirst.Models.Cadastro
    <div>
       @Html.EditorFor(model => Model.Fisica)
    </div>

PARTIAL PHYSICAL VIEW

                    @model CodeFirst.Models.Fisica
                    <div class="col-xs-12 col-md-1-5 marginCimaBaixo">
                        @Html.LabelFor(model => model.RG)
                        @Html.EditorFor(model => model.RG)
                        @Html.ValidationMessageFor(model => model.RG)
                    </div>

CONTROLLER EDIT POST

    public ActionResult Edit(Cadastro cadastro)
    {
       db.Entry(cadastro.Fisica).State = EntityState.Modified;
    }

Thank you.

  • Now you’ve made the right inheritance. I’ll answer.

  • Dear Lord, almost a thousand points and you still don’t know what "thank you" is done with +1? That’s noise my dear, Furlan, is there in the 1st guide that we should read when you start using this site: [tour] ---> não tem bla bla bla. Thank you for understanding!

1 answer

6

If Fisica drift Pessoa, It’s not right here:

@model CodeFirst.Models.Fisica
<div>
   @Html.EditorFor(model => Model.Fisica)
</div>

Use @Html.Partial instead of @Html.Editor:

@model CodeFirst.Models.Fisica

<div>
   @Html.Partial("_MinhaPartialPessoaFisica", Model)
</div>

And the Partial:

@Model CodeFirst.Models.Fisica

<div class="col-xs-12 col-md-1-5 marginCimaBaixo">
    @Html.LabelFor(model => model.RG)
    @Html.EditorFor(model => model.RG)
    @Html.ValidationMessageFor(model => model.RG)
</div>

Controller:

public ActionResult Edit(Fisica fisica)
{
   db.Entry(fisica).State = EntityState.Modified;
}

This here is terribly wrong, and it shows that you don’t understand how inheritance works:

public class Cadastro
{
    public Pessoa Pessoa { get; set; } 
    public Fisica Fisica { get; set; }
}

If Fisica drift Pessoa, you don’t need to have Fisica and Pessoa. By the way, you don’t even have to have this Viewmodel Cadastro because how Fisica already drifting Pessoa, all fields of Pessoa already exist in Fisica.

Therefore, its View Edit should look like this:

@model CodeFirst.Models.Fisica
<div>
   @Html.Partial("_MinhaPartialPessoaFisica", Model)
</div>
  • forgot to ask a few questions, I’ll edit

  • please look again

  • 6

    I’m already giving up on answering you. Really, really.

Browser other questions tagged

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