Asp.Net MVC: Why is my view passing NULL models back to my controller?

Its null because your model contains a property named gl_code and you have also named the parameter for your model gl_code in the POST method.

Change the name of one or the other and the model will bind correctly.

What is happening internally is that the form submits a name/value pair for each successful form control, in your case gl_code=someValue. The DefaultModelBinder first initializes a new instance of your model. It then reads the form values and finds a match for the property in your model and sets it to someValue. But it also finds a match in the method parameters and tries set the value of the parameter to someValue, which fails (because you cannot do gl_code gl_code = "someValue";) and the model becomes null.

Leave a Comment