I tell you that I have 2 controllers, one for the GET and one for the POST that I am going to edit to make it easier to understand:
// GET: Compras/Create
public ActionResult Create(int id)
{
ComprasCreateModel compra = new ComprasCreateModel();
compra.ClienteId = id;
compra.NombreCliente = db.Clientes.Find(id).Nombre;
return View(compra);
}
// POST: Compras/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ComprasCreateModel compra)
{
if (compra.ClienteId == 0)
return View(A);
else
return View(B);
}
And it happens, that my View Create presents a form to fill:
model.Age
model.Phone
Therefore, the only thing that is changed from the model in the View is those properties. And clicking the submitButton calls the POST handler.
But if a purchase model with the CustomerId and CustomerName properties was passed from the GET, why is the final result showing me View A?
That is, the POST of Create receives purchase.CustomerId with value 0, and purchase.CustomerName with null value.
It is as if the form is creating a new model with the data that was filled, thus forgetting the previous model that was passed as a parameter to the View.
What am I still not understanding about this?
For the complete model to be part of the model binding you must assign all the properties to the html object that are part of the request body
If for example you define a
@Html.LabelFor()
this value does not apply in the request and the value will not be sent to the server for model binding, that is why the@Html.HiddenFor()