I am posting a form to an MVC4 controller-action. I am iterating over the FormCollection as so:

    [HttpPost]
    public ActionResult Details(string nctId, FormCollection collection)
    {
        foreach (var key in collection.AllKeys)
        {
            var value = Request.Form[key]; // <-- breakpoint here
        }

        return RedirectToAction("Details", new { nctId = nctId });
    }

I (think I) am submitting a form with the following select inputs:

  • name="ddlOne", id="ddlOne", value="ValueOne"
  • name="ddlTwo", id="ddlTwo", value="ValueTwo"
  • name="ddlThree", id="ddlThree", value="ValueThree"

However, when I place a breakpoint on the indicated line, I see the following results:

  • key: "ddlOne", value: null
  • key: "ddlTwo", value: "ValueOne"
  • key: "ddlThree", value: "ValueTwo"

As you can see, the values are "off by one."

What might cause this issue?

有帮助吗?

解决方案

The break-point happens before the assignment occurs.

Press F10 once to advance execution over the assignment and see the updated (and correct) pairing. Alternatively, verify that all is well by inspecting Request.Form directly.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top