MVC message not populating the model

I canโ€™t understand why my model is not filling. All data is placed in Request.Form , but the model is actually null .

Consistent with this response to a model with a collection that does not populate upon postback

In other words, (...) If there are no required fields or values โ€‹โ€‹presented in such a way that they cannot be converted to the type of the required field, then the whole object will remain empty

I have changed several types of values, but I cannot get it to work.

Here is my model:

 public class AddModel { //Get properties public Vehicle vehicle; //Post properties [Required(ErrorMessage = "Please enter a start date")] public DateTime StartDate; public int? StatusCode; public SelectList StatusCodes() { ... } } 

Can you think why it is not populated?

+4
source share
2 answers

Creating AddModel participants Properties - adding get; set; get; set; in the fields should solve your problem

+4
source

According to @archil answer you should make your public variables. Although this may work with types, you will run into problems as soon as you add complexity. Of course, for classes, but it is also possible for null types.

Interacting models use reflection to analyze form fields in the model, and reflection works differently in properties for public variables - in the case of these models, the differences are likely to cause a failure here .

Hope this helps - and the hat tip on @archil to answer this (perhaps) correctly, much earlier than me!

+2
source

All Articles