Post null no Edit MVC

Asked

Viewed 319 times

1

Hey, I got this situation, man Edit returns a group of Classes filled for editing. In my View the data is filled correctly, however, when I edit a field and have it saved in the Edit (Post) my Model zero arrives, that is to say, NULL. I’ve already found out it’s because of Partial, have some other way for me to accomplish?

below follows 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 ARRIVES NULL !

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

REGISTER 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 mine Class

instead of using

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

use:

@Html.EditorFor(model => Model.Fisica)
  • can also post your class Cadastro?

  • yes @Jedaiasrodrigues I will change the question with the class, thank you

  • You can make the following change and do if the object continues to null [HttpPost]&#xA; [ValidateAntiForgeryToken]&#xA; public ActionResult Edit([FromUri]Cadastro cadastro)

  • @Joserocha I’ll try here, just a moment

  • @Furlan I was wrong where this [FromUri] should be [FromBody] sorry. Since it is a Post method the date will by body not by Uri

  • @Joserocha this command: [FromBody] serves to MVC? or is it just for WebAPI

  • @Furlan is only for HttpRequestMessage normally used to WebApi, but I noticed that the method is a ActionResult

  • Could use an Enum instead of Type = "F" hauhauha

Show 4 more comments

1 answer

1


SOLUTION

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

instead of using

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

use:

@Html.EditorFor(model => Model.Fisica)

Browser other questions tagged

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