views:

177

answers:

1

I am using ASP.NET MVC2 and Entity Framework. I am going to simplify the situation a little; hopefully it will make it clearer, not more confusing!

I have a controller action to create address, and the country is a lookup table (in other words, there is a one-to-many relationship between Country and Address classes). Let's say for clarity that the field in the Address class is called Address.Land. And, for the purposes of the dropdown list, I am getting Country.CountryID and Country.Name.

I am aware of Model vs. Input validation. So, if I call the dropdown field formLand - I can make it work. But if I call the field Land (that is, matching the variable in Address class) - I am getting the following error:

"The parameter conversion from type 'System.String' to type 'App.Country' failed because no type converter can convert between these types."

OK, this makes sense. A string (CountryID) comes from the form and the binder doesn't know how to convert it to Country type. So, I wrote the converter:

namespace App {
    public partial class Country {
        public static explicit operator Country(string countryID) {
            AppEntities context = new AppEntities();
            Country country = (Country) context.GetObjectByKey(
                new EntityKey("AppEntities.Countries", "CountryID", countryID));
            return country;
        }
    }
}

FWIW, I tried both explicit and implicit. I tested it from the controller - Country c = (Country)"fr" - and it works fine. However, it never got invoked when the View is posted. I am getting the same "no type converter" error in the model.

Any ideas how to hint to the model binder that there is a type converter? Thanks

+1  A: 

A type converter is not the same as an explicit or implicit conversion, it's an object that converts values between various types.

I think you need to create a class inherited from TypeConverter that converts between Country and other types, and apply the TypeConverterAttribute to your class to specify the converter to use :

using System.ComponentModel;

public class CountryConverter : TypeConverter
{
    // override CanConvertTo, CanConvertFrom, ConvertTo and ConvertFrom
    // (not sure about other methods...)
}

[TypeConverter(typeof(CountryConverter))]
public partial class Country
{

...

}
Thomas Levesque
Wow! That is a huge step in the right direction... I didn't know about TypeConverter. Thank you very much. I also looked at Microsoft's HowTo: http://msdn.microsoft.com/en-us/library/ayybcxe5.aspxNow an interesting thing happens. Apparently, EF generated CountryConverter, but it is derived from EntityObject and apparently doesn't have anything to do with TypeConverter (weird, if true). So, I created class LandConverter, decorated Country with [TypeConverter(typeof(LandConverter))].(continued...)
Felix
I am still not getting to the breakpoint in LandConverter:CanConvertFrom or LandConverter:ConvertFrom. However, the exception sounds a little different:"The parameter conversion from type 'System.String' to type 'App.CountryConverter' failed because no type converter can convert between these types."Note CountryConverter which is the generated class. Not my LandConverter. So, now I am even more confused.(hm - only 1 comment allowed per 15 sec ;)
Felix