显式转换并不在默认模型绑定工作 [英] Explicit casting doesn't work in default model binding

查看:79
本文介绍了显式转换并不在默认模型绑定工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET MVC2和Entity Framework。我要简化局面一点点;希望这将使其更清晰,更不能混淆!

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!

我有一个控制器行动创造地址,并在全国是一个查找表(换句话说,有国家和Address类之间的一个一对多的关系)。比方说,为清楚起见,在Address类的字段被称为Address.Land。而且,对于下拉列表的目的,我得到Country.CountryID和Country.Name。

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.

我知道<一个href=\"http://bradwilson.typepad.com/blog/2010/01/input-validation-vs-model-validation-in-aspnet-mvc.html\"相对=nofollow>模型与输入验证。所以,如果我叫下拉场的 formLand 的 - 我可以使它发挥作用。但是,如果我叫现场的土地(即匹配Address类变量) - 我收到以下错误:

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:

从类型参数转换
  'System.String'输入'App.Country
  失败,因为没有类型转换器可
  这些类型之间的转换。

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

OK,这是有道理的。一个字符串(CountryID)来自于形式和粘合剂不知道如何将其转换为国家类型。所以,我写的转换器:

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,我想这两个显性和隐性。我测试了它从控制器 - C国=(国家)FR - 它工作正常。然而,当查看发布它从来没有得到调用。我得到的模型相同的无类型转换器的错误。

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

推荐答案

一个类型转换器是不一样的显式或隐式转换,它是多种类型之间转换值的对象。

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

我想你需要创建从的TypeConverter 继承一个类,国家和其他类型之间进行转换,并应用在 TypeConverterAttribute 到您的类指定该转换器的使用方法:

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
{

...

}

这篇关于显式转换并不在默认模型绑定工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆