Post null in the MVC edit

1

Hello, I have the following situation, my Edit returns a group of Classes filled for editing. In my View the data is filled correctly, however, when I edit a field and send save in Edit (Post) my Model arrives zeroed, that is, NULL . I have already discovered that it is because of Partial , do you have any other way for me to perform?

Below is my code:

CONTROLLER (GET)

    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }

        var cadastro = new Cadastro(); // Inicializa o Grupo de Classes...

        cadastro.Pessoa = new Pessoa();
        cadastro.Fisica = new Fisica();
        cadastro.Juridica = new Juridica();

        cadastro.Pessoa = db.Pessoas.Find(id);

        if (cadastro.Pessoa == null)
        {
            return HttpNotFound();
        }

        if (cadastro.Pessoa.Tipo == "F")
        {
            cadastro.Fisica = db.Fisicas.Find(id);
        }
        else
        if (cadastro.Pessoa.Tipo == "J")
        {
            cadastro.Juridica = db.Juridicas.Find(id);
        }

        return View(cadastro);
    }

VIEW EDIT

@model CodeFirst.Models.Cadastro

@using (Html.BeginForm())
{

@Html.AntiForgeryToken()

<div class="form-horizontal">
    <div class="form-horizontal">

        @Html.HiddenFor(model => Model.Fisica.IdPessoa)
        @Html.HiddenFor(model => Model.Juridica.IdPessoa)

        @if (Model.Pessoa.Tipo == "F") {
            <p>Pessoa Fisica:</p>
            <input type="radio" name="radio" value="F" checked="checked" class="radio">
            <input type="radio" name="radio" value="J" class="radio">
            <script> $('.boxf').show(); </script>
        } else
        if (Model.Pessoa.Tipo == "J")
        {
           <p>Pessoa Juridica:</p>
            <input type="radio" name="radio" value="F" class="radio">
            <input type="radio" name="radio" value="J" checked="checked" class="radio">
            <script> $('.boxf').show(); </script>
        }

        @Html.LabelFor(model => model.Pessoa.Documento)
        @Html.EditorFor(model => model.Pessoa.Documento)

        <div class="boxf">
            @Html.Partial("~/Views/Shared/EditorTemplates/_PessoaFisica.cshtml", Model.Fisica)
        </div>

        <div class="boxj">
            @Html.Partial("~/Views/Shared/EditorTemplates/_PessoaJuridica.cshtml", Model.Juridica)
        </div>
        <div class="row marginCimaBaixo text-right">
             <input type="submit" class="btn btn-success" value="Salvar Cadastro"   />
        </div>
    </div>
   </div>  
}

CONTROLLER (POST), HERE THE REGISTRATION IS NULL!

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(Cadastro cadastro)
    {
    }

CADASTRO CLASS

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

SOLUTION

Very simple solution, my EditorTemplates has to be the same name as my Class

instead of using

@Html.Partial("~/Views/Shared/EditorTemplates/_PessoaFisica.cshtml", Model.Fisica)

use:

@Html.EditorFor(model => Model.Fisica)
    
asked by anonymous 09.12.2015 / 12:31

1 answer

1

SOLUTION

Very simple solution, my EditorTemplates has to be the same name as my Class

instead of using

@Html.Partial("~/Views/Shared/EditorTemplates/_PessoaFisica.cshtml", Model.Fisica)

use:

@Html.EditorFor(model => Model.Fisica)
    
09.12.2015 / 19:05