Proper way of using FormCollection in ASP.NET MVC2 Create Method?
Asked Answered
A

2

8

I am currently developing an application with the new ASP.NET MVC2 framework. Originally I started writing this application in the ASP.NET MVC1 and I'm basically just updating it to MVC2.

My problem here is, that I don't really get the concept of the FormCollection object vs. the old Typed object.

This is my current code:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection collection)
{
    try
    {
        Member member = new Member();
        member.FirstName = collection["FirstName"];
        member.LastName = collection["LastName"];
        member.Address = collection["Address"];

        // ...

        return RedirectToAction("Details", new { id = member.id });
    }
    catch
    {
        return View("Error");
    }
}

This is the Code from the MVC1 application:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Member member)
{
    try
    {
        memberRepository.Add(member);
        memberRepository.Save();

        return RedirectToAction("Details", new { id = member.id });
    }
    catch
    {
    }
    return View(new MemberFormViewModel(member, memberRepository));
}

What are the benefits of switching to FormCollection in MVC2 and more importantly - how is it used properly?

Antifederalist answered 28/1, 2010 at 15:59 Comment(2)
I see none, is the old model broken? Why switch?Oshaughnessy
No it's not broken.. it was looking like the old model strongly typed approach was abandoned as the new Controllers come with a FormCollection instead of a strongly typed object.Antifederalist
A
11

You had the FormCollection object in v1 as well. But it is more preferred to use a typed object. So if you are already doing that, then continue doing so.

Albanian answered 28/1, 2010 at 16:6 Comment(1)
+1 Yes, The FormCollection has been there since the beginning. If it ain't broke, don't fix it!Swanherd
O
0

By using FormCollection, you wind up manually matching your post data or query string key/values into values to use in your code using string typing (resulting in stringly-typed code), when instead the built-in Model Binding can do this for you if you use form models, aka "typed objects."

I think by using the FormCollection, you would probably also lose the ability to use the handy Data Annotation (slash Validation) attributes on your model objects as well, which are designed for use with typed object model binding.

Additionally, unit testing can become much more cumbersome once you start touching your controller.Request.Form. You might find yourself having to mock and setup an HttpContextBase, and an HttpRequestBase just to have that mock request's .Form property return the NameValueCollection that you are wanting your test to see. Contrast this to letting model binding do the work for you such as:

  // Arrange
  var myModel = new MyModel( Property1 = "value1", Property2 = "value2");
  // Act
  var myResult = myController.MyActionMethod(myModel);
  // Assert
  // whatever you want the outcome to be

In summary, I would recommend against using FormCollection to the maximum extent possible.

Officialism answered 12/1, 2012 at 20:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.