Upgrading ASP.NET MVC to ASP.NET MVC3 FormCollection seems completely different

2k views Asked by At

Hullo,

I am trying to update a website from ASP.NET MVC1 to ASP.NET MVC3 and from VS2008 to VS2010. So far everything seems to be going OK except the class FormCollection seems to be completely different across the two visual studios.

I've noticed that in VS2008 there is a FormCollection for version 1 and version 2 (presumably MVC1 and MVC2) and I can't see any big changes with them, but in VS2010 the only FormCollection class there is is a sealed class which is causing me grief as I have a class from before that inherits from FormCollection.

Anyone know if the FormCollection I used to know and love has moved or if I should just rewrite everything using the class that inherits it (a lot of stuff)?

Regards, Harry

1

There are 1 answers

1
Harold On BEST ANSWER

Worked my way around it now. Instead of inheriting FormCollection now it has it's own field 'form' which is a FormCollection instead and it's commands just act on that.

Edit:

People were asking for the codes, so here's the old class:

using System;
using System.Linq.Expressions;
using System.Web.Mvc;
using OpenProjects.Core;

namespace ProjectSupport.Web.Test
{
    public class FormFor<TEntity> : FormCollection where TEntity : class
    {
        public FormFor<TEntity> Set<TReturn>(Expression<Func<TEntity, TReturn>> property, string value)
        {
            this[property.PropertyName()] = value;

            return this;
        }
    }
}

And here's how it is now:

using System;
using System.Linq.Expressions;
using System.Web.Mvc;
using OpenProjects.Core;

namespace ProjectSupport.Web.Test
{
    public class FormFor<TEntity> where TEntity : class
    {
        private FormCollection form;


        public FormFor()
        {
            form = new FormCollection();
        }

        public FormFor<TEntity> Set<TResult>(Expression<Func<TEntity, TResult>> property, string value)
        {
            form.Add(property.PropertyName(), value);

            return this;
        }

        public FormCollection ToForm()
        {
            return form;
        }
    }
}

For clarification on why this was being used rather than model binders, this was being used only for testing to easily mock up forms quickly and easily.