0

How can I inspect the post data that are being submited In my application? I want see If there are any data In the input fields when the post are submited.

Html:

<div id="addBox" style="display: none; margin-top: 10px;">
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        <div class="form-group">
            @Html.Label("Descrption", "Att göra"):
            @Html.TextBox("Description", null, new { id = ViewBag.ListContainerID })
            <input type="submit" value="Ok" class="btn btn-default" />
        </div>       
    }
</div>

Controller:

[HttpPost]
    [ActionName("Details")]
    public ActionResult AddTask(FormCollection values)
    {
        var id = values["id"];
        var desc = values["Description"];

        /*Task task = new Task
        {
            Description = desc,
            ListContainerID = id
        };

        db.Tasks.Add(task);
        db.SaveChanges();*/
        return RedirectToAction("Details");
    }

I have tried to set a breakpoint beside the id and desc, but can't find the information about what the input field contains.

Gnqz
  • 3,292
  • 3
  • 25
  • 35
Bryan
  • 3,421
  • 8
  • 37
  • 77
  • What "information" are you looking for? What does `FormCollection["key"]` not provide that you need? – CodeCaster Nov 12 '15 at 14:35
  • You can look at what's being sent in `values`, you can open up DevTools (F12) in Chrome, you can use Fiddler, yada yada yada – ragerory Nov 12 '15 at 14:35
  • @CodeCaster: What the user typed In. If I write "FIsh" and post, I want to see If the field contains Fish. But where can I check the value of the input field? – Bryan Nov 12 '15 at 14:36
  • That depends on the name of the input field. If the name is `Foo`, then its contents, when POSTed, can be read from `values["Foo"]`. – CodeCaster Nov 12 '15 at 14:37
  • @CodeCaster: And how can I read from values["foo"]? How do I do that? – Bryan Nov 12 '15 at 14:38
  • Just as you have it in code... like `var fooField = values["Foo"]`. It's very unclear what you're asking. What do you mean by "read"? Anyway @Gnqz's answer is spot-on, if you don't call the overload where you specify the `FormMethod`, the form will default to GET and the `FormCollection` will be empty. – CodeCaster Nov 12 '15 at 14:38
  • @CodeCaster: I don't know how can I make It any clearer? I want to see the values posted by the user in the input-fields? In javascript, In a ajax post request, you can write console.log, to the what the user are posting. I want to do the same here, without Javascript. – Bryan Nov 12 '15 at 14:41
  • @Bryan Aren't you getting the values once the method is specified? Also provide IDs for the fields. – Gnqz Nov 12 '15 at 14:45
  • @Gnqz: What do u mean? – Bryan Nov 12 '15 at 14:46
  • Didn't see the name of the textbox. Do you get to the controller method? Do you want to check the value of the field to perform some check or just to see if they are clear or whatever? – Gnqz Nov 12 '15 at 14:49
  • @Gnqz: I just want to check If the fields contains any values when It hits the controller method. – Bryan Nov 12 '15 at 14:57
  • Well, there might be a way without using JS, but I don't know it. JS is perfect for that purpose. Don't know why you wouldn't like to do it that way. – Gnqz Nov 12 '15 at 14:59
  • I'll try one last time. Explain what you mean by "read" and "see". Do **you** want to see this? When? When debugging? Do you know what breakpoints are? Or do you want to show the variables' contents to the user? Do you know how views work then? – CodeCaster Nov 12 '15 at 15:01
  • @Gnqz: Because for example in PHP, you just can write var_dump($_POST) to the all the data. I thought It could be the same In ASP.NET – Bryan Nov 12 '15 at 15:01
  • Well, php has nothing to do with ASP. PHP is a scripting language. The advantage of ASP is that you could use C#, which is quite clear and the power of the .NET framework. Just use JS and get your job done. – Gnqz Nov 12 '15 at 15:05
  • @CodeCaster: In PHP you can use var_dump($_POST) to see information about the POST-data after you hit a submit-button. I want to do the EXACT same thing in ASP.NET. – Bryan Nov 12 '15 at 15:05
  • @CodeCaster: As I wrote In my question, I have tried to use the debugger, but can't find the value Inserted by the user. I hover my variable, and It gives me a List to expand, containing "All keys", "Count", "IsReadOnly". But where should the Value be? – Bryan Nov 12 '15 at 15:14
  • If that is your actual problem, see [my comment of 36 minutes ago](http://stackoverflow.com/questions/33673831/inspect-post-data-values#comment55120681_33673831) and [@Gnqz's answer of around that time](http://stackoverflow.com/a/33673911/266143). You're not POSTing your form, so your FormCollection is empty. – CodeCaster Nov 12 '15 at 15:15

2 Answers2

1

In PHP you can use var_dump($_POST) to see information about the POST-data after you hit a submit-button. I want to do the EXACT same thing in ASP.NET

No, you don't want to do that.

Visual Studio has a great debugger that you can use. Set breakpoints and inspect the relevant variables. That way you don't have to change your code when debugging, and you can't forget to remove it afterwards.

You could use the ViewBag to transfer variables from your controller to your view:

// Set in Controller:
ViewBag.TestingVariable1 = values["Foo"];

// Render in View:
@ViewBag.TestingVariable1

Still, this is not recommended. You're changing your application to debug, which is never advisable.

If you insist, look at Response.Write():

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

Why haven't you added the parameters of the Html.BeginForm(). It should be:

Html.BeginForm("AddTask","ControlerName...",FormMethod.Post)
{}

To check the submit values, you could use ajax and do an async call of the controller method and use alerts/logs to check/print the values (jquery).

Gnqz
  • 3,292
  • 3
  • 25
  • 35
  • The view is likely returned by `AddTask` action with `[HttpGet]`. `BeginForm()` uses the current routing data. – Igor Nov 12 '15 at 14:48
  • If he uses Http.Post to return that view, I don't undestand anything from his question then :D – Gnqz Nov 12 '15 at 14:52