In my previous post I discussed how values sent from the browser are bound to objects in the action methods of a controller.
If binding is not working as expected you need to examine the html element names and the types you are binding to, I showed how the FormCollection can be of help in this regard.
In this post I will show how to use Glimpse to examine how binding is occurring.
I should at this point say that I am one of the contributors to the Glimpse project.
Add the Glimpse Mvc 4 package to your project.
Start your application; in the browser open glimpse.axd from the root and turn glimpse on by clicking the Turn Glimpse On button.
Browse to the page where you want to see how binding is working.
In the example, edit employee 1, John Jones. Make some changes to the employee and add a comment to the reason for edit and save.
You’ll be brought back to the index page listing all employees.
At the bottom of the browser you should have a Glimpse bar showing some statistics about the page.
Open Glimpse fully by clicking the ‘g’ in the right corner and click History on the far right.
Here you will see all the recent requests, we are interested in the most recent POST.
Click inspect on that request, the POST request http://localhost:55809/Employee/Edit?EmployeeID=1. The tabs along the top of Glimpse have been refreshed with data from that request.
Open the Model Binding tab to see how the values from your request have been bound to your types in the action method.
Here you can see parameters to the action method, their type and and what they were bound to.
If you see a parameter that has “–” (two dashes) in the Value column, nothing was sent to the action method that could be bound to a parameter on the action method.
Now take a look at the Request tab and the form variables. Note how the keys of the form variables match the parameters in the model binding tab.
This is the easiest way I have found to understand how model binding works in MVC and fix problems.