Asp.Net MVC: Why is my view passing NULL models back to my controller?
Solution 1:
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
.
Solution 2:
It appears you have a property on your view model called gl_code. In your controller, you also refer to the view model as gl_code.
Try changing this.
public async Task<ActionResult> Edit(gl_code gl_code)
To
public async Task<ActionResult> Edit(gl_code model)