views:

1088

answers:

2

Hello everybody!

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?

+4  A: 

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.

Mattias Jakobsson
+1 Yes, The FormCollection has been there since the beginning. If it ain't broke, don't fix it!
Ricardo
A: 

Let's say the Action below is exposed via REST service and is called from a different appliation how would it handle the posted data/object?

[AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(Member member) { .... }

Shuaib