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
?– Jedaias Rodrigues
yes @Jedaiasrodrigues I will change the question with the class, thank you
– Furlan
You can make the following change and do if the object continues to null
[HttpPost]
 [ValidateAntiForgeryToken]
 public ActionResult Edit([FromUri]Cadastro cadastro)
– Jose Rocha
@Joserocha I’ll try here, just a moment
– Furlan
@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– Jose Rocha
@Joserocha this command:
[FromBody]
serves toMVC
? or is it just forWebAPI
– Furlan
@Furlan is only for
HttpRequestMessage
normally used toWebApi
, but I noticed that the method is aActionResult
– Jose Rocha
Let’s go continue this discussion in chat.
– Furlan
Could use an Enum instead of Type = "F" hauhauha
– Rod