yoozer8
yoozer8

Reputation: 7489

Is there an easy way to view the contents of a FormCollection?

When testing/debugging an ASP.NET MVC application, it's common to submit a form and then check all of the name/value pairs to make sure

  1. All of the expected keys are present
  2. All of the expected keys have the expected values

Debugging in Visual Studio is great for checking if a single variable (or even a simple object) contains the expected value(s), and as far as a FormCollection, it's pretty easy to check the presence of the keys. However, checking the key/value pairings in a FormCollection is a huge hassle. Is there a simple way to get Visual Studio to list the keys and their values side-by-side for a quick check?

Upvotes: 2

Views: 2756

Answers (3)

Tawab Wakil
Tawab Wakil

Reputation: 2333

Here is a method that outputs the collection to the Immediate window in a readable key/value pair format, one pair per line of output:

private void DebugWriteFormCollection(FormCollection collection)
{
    foreach (string key in collection.Keys)
    {
        string value = collection.GetValue(key).AttemptedValue;
        string output = string.Format("{0}: {1}", key, value);
        System.Diagnostics.Debug.WriteLine(output);
    }
}

Add that to your code and set a breakpoint. When you hit the breakpoint, call the method from the Immediate window:

DebugWriteFormCollection(collection);

Result:

Key1: Value1
Key2: Value2
Key3: Value3
etc.

Upvotes: 0

codingbiz
codingbiz

Reputation: 26396

Just a quick custom check

    public void Edit(FormCollection team)
    {
        System.Text.StringBuilder st = new System.Text.StringBuilder();

        foreach (string key in team.Keys)
        {
            st.AppendLine(String.Format("{0} - {1}", key, team.GetValue(key).AttemptedValue));
        }

        string formValues = st.ToString();
        //Response.Write(st.ToString());
    }

You can then place your mouse on formValues to check the key-value. Clicking the magnifier would reveal the Key-Values

enter image description here

Upvotes: 6

Oded
Oded

Reputation: 499382

Take a look at Glimpse, it is on nuGet. It exposes lots of information and is invaluable with AJAX and MVC development.

At its core 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.

Upvotes: 1

Related Questions