55

Can you give me any general advice on how to debug ASP.NET MVC Binding?

When everything works as expected, ASP.NET MVC is great. But if something does not, like something doesn't bind for some unknown reason, I find it hard to trace down the problem and find myself spending hours tracking down a seemingly simple problem.

Let's imagine you land in a controller method like this:

[HttpPost]
public ActionResult ShipmentDetails(Order order)
{
    //do stuff
}

Let's further imagine that the Order class looks like this:

public class Order
{
    public decimal Total {get; set;}
    public Customer Customer {get; set;}
}

public class Customer
{
    public string Name {get; set;}
    public string Phone {get; set;}
}

What are good places to start when Order in the controller method is not bound correctly? What are good places to start when only parts of the Order are bound correctly?

6 Answers 6

26

Although @russ's answer is useful and will sometimes be necessary, both options seem a little low level when the main question is more about the big picture. Thus, I'd recommend Glimpse.

From its about page:

… Glimpse allows you to debug your web site or web service right in the browser. Glimpse allows you to "Glimpse" into what's going on in your web server. In other words what Firebug is to debugging your client side code, Glimpse is to debugging your server within the client.

And since you've specifically asked about data binding, you'll want to look at the binding tab documentation. You'll be able to see, again from the docs:

  1. Ordinal: Order in which the MVC Model Binding infrastructure attempted to bind the available data
  2. Model Binder: Model Binder that was used in a given scenario
  3. Property/Parameter: Name of the thing that the Binder was trying to bind
  4. Type: Type of the thing that the Binder was trying to bind
  5. Attempted Value Providers: Providers that the Binder attempted to use to get a given value (and whether it was successful)
  6. Attempted Value: The actual value that the provider has to work with (post type conversation, etc.)
  7. Culture: The culture that was used to parse the raw value Raw Value: The raw value that the provider has to work with (pre type conversation, etc.)

See the quick start. Briefly:

  1. Install the glimpse.mvc3 package
  2. Go to http://yourhost/yourapp/Glimpse.axd and "turn it on."
  3. Click on the glimpse icon on the bottom right of any view in your app for details.
Sign up to request clarification or add additional context in comments.

Comments

22

As Darin has suggested, start with inspecting what is being sent from the client to the server using something like Firebug, Fiddler, or other web debugging proxy tool.

Failing that, you might want to step through the source code to see what's happening during binding.

Two ways that I can recommend doing this are

  1. Include the System.Web.Mvc source code project in your application and reference this. This is good for learning but probably not recommended for a commerical application.

  2. Download the symbols for System.Web.Mvc from the Microsoft Symbol servers, change your settings to be able to debug framework source code and set a break point appropriately to step through.

4 Comments

"set a break point appropriately" Where is this for the model binder?
You'd want to set a breakpoint on the MVC framework's DefaultModelBinder class' public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) method
After enabling/downloading the symbols, how can you actually get to the source for DefaultModelBinder in order to set the breakpoint?
NM, entering the breakpoint manually works although it did give a message saying it couldn't find the function (or something similar).
19

In my case looking at the ModelState property in the controller method provided the answers why the modelbinding was failing.

enter image description here

2 Comments

It looks like this is specific to ASP.NET Core
I just tried in ASP.NET Web Api, I can able to see the ModelState in the "Local window" of my Web API method.
3

A good place to start is download and install FireBug and see what gets posted from the client to the server. Then you will see what's missing, incorrect, ... Blog posts such as Model Binding to a List are good reads as well to get acquainted with the proper syntax that the default model binder uses.

3 Comments

But it doesn't tell you what mvc binding does with the data... is there a way to track the model binding process ?
@Dani, what do you want to know about the model binding? It's pretty straigtforward: An input field named Customer.Name will bind to the Name property of a complex Customer property of a model. There is nothing special about it. And once you get the concept of lists and collections you know everything there is to know for how model binding works. When it doesn't FireBug will show you the request and you will immediately know why a property hasn't been bound: it's because either it is missing from the request, has an invalid name or invalid format.
I guess what I'm missing is the collection and lists, and what to do with new items. the samples I've used until now adds rows to grid with random id, and nothing bounds in my code, I've seen one example that tries to re-number, but the original example (steve Sandersen) works without it.....
2

From Visual Studio side:

  • Set a breakpoint on entry to your endpoint.
  • Open the Immediate via the Debug menu option.

Type in ModelState.Keys.ToList()

  • This will show the binding errors by name/key.

Better yet type in ModelState.Values.ToList()...

Model State Values display

1 Comment

.NET Core only?
0

Put a breakpoint in your controller-method and make a watch for Request.Params to see what is actually coming in to the controller in terms och parameters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.