Display Name
Display Name

Reputation: 15101

What is FormCollection argument for? Is it a dummy argument?

Up to now I have no idea why does VS provide FormCollection argument by default?

    public ActionResult Edit(int id)
    {
        Dinner dinner = dinnerRepository.GetDinnerById(id);

        if (dinner == null)
            return View("NotFound");
        else
            return View(dinner);
    }

    [HttpPost]
    public ActionResult Edit(int id, object dummy/*, FormCollection collection*/)
    {
        Dinner temp = dinnerRepository.GetDinnerById(id);

        if (TryUpdateModel(temp))
        {
            dinnerRepository.Save();

            return RedirectToAction("Details", new { id = temp.DinnerId });
        }
        else
            return View(temp);
    }

EDIT 1: In my experiment, any arguments other than id are dummy because they never be used in httppost Edit action method.

EDIT 2: Does TryUpdateModel use FormCollection behind the scene?

Upvotes: 0

Views: 1262

Answers (3)

Justin Niessner
Justin Niessner

Reputation: 245429

The FormCollection is how ASP.NET provides you access to the values that were just posted to your page.

You could also use a strongly typed argument and then ASP.NET MVC would use the FormCollection internally to create your strongly typed object.

Upvotes: 2

Matt Greer
Matt Greer

Reputation: 62037

If your app receives a POST, then 99.99% of the time it will come from an HTML form. The FormsCollection gathers all the values in the form for you.

In ASP.NET MVC you are almost always better off using strongly typed objects though. The DefaultModelBinder will create them for you most of the time, and you can implement IModelBinder if needed if the default one doesn't do what you need.

Upvotes: 3

Aliostad
Aliostad

Reputation: 81660

FormCollection contains your form state coming back to your server.

If you need any custom operation on the processing of your data you use FormCollection. Otherwise you can happily remove it.

I am using it heavily for a hierarchical model processing.

Upvotes: 2

Related Questions