Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for debugging ASP.NET MVC Binding

Tags:

asp.net-mvc

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?

like image 324
mklein Avatar asked Jan 10 '11 20:01

mklein


People also ask

Does MVC use data binding?

MVC doesn't use data bindings like old web api. You have to use model bindings in a MVC or MVVM approach.

What is binding in ASP.NET MVC?

ASP.NET MVC model binding allows mapping HTTP request data with a model. It is the procedure of creating . NET objects using the data sent by the browser in an HTTP request. Model binding is a well-designed bridge between the HTTP request and the C# action methods.


3 Answers

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.
like image 141
Kaleb Pederson Avatar answered Oct 19 '22 08:10

Kaleb Pederson


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.

like image 32
Russ Cam Avatar answered Oct 19 '22 07:10

Russ Cam


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

enter image description here

like image 18
Flores Avatar answered Oct 19 '22 09:10

Flores