如何解决错误 "Type provided must be an Enum. Parameter name: enumType"

How to solve the error "Type provided must be an Enum. Parameter name: enumType"

所以我一直在使用Pay Simple SDK。我收到这个错误。有谁知道如何解决这个问题?

var customerPayment = new NewCustomerPayment<CreditCard>

所以我的模型在 Pay Simple SDK 中。这是用于支付交易。

控制器代码:

[HttpPost]
[ValidateAntiForgeryToken]
public async System.Threading.Tasks.Task<ActionResult> Create(FormCollection formCollection) {
    string username = "jnjk";
    string apiKey = "khkh";
    string baseUrl = "https://sandbox-api.paysimple.com";
    var settings = new PaySimpleSdk.Models.PaySimpleSettings(apiKey, username, baseUrl);

    var paymentService = new PaymentService(settings);

    var customerPayment = new NewCustomerPayment<CreditCard>()
    {

        Customer = new Customer
        {
            FirstName = formCollection["FirstName"],
            LastName = formCollection["LastName"],
            BillingAddress = (Address)Enum.Parse(typeof(Address), formCollection["BillingAddress"])
        },

        Account = new CreditCard
        {
            CreditCardNumber = formCollection["CreditCardNumber"],
            ExpirationDate = formCollection["ExpirationDate"],
            Issuer = (Issuer)Enum.Parse(typeof(Issuer), formCollection["Issuer"])
        },

        Payment = new Payment
        {  
            Amount = int.Parse(formCollection["Amount"]),
            Cvv = formCollection["Ccv"]
        }

    };

    var newCustomerPayment = await paymentService.CreateNewCustomerPaymentAsync(customerPayment);

    return View();
}

来自 sdk 的模型

这是客户模型中的账单地址..

  public Address BillingAddress { get; set; }

这是地址

  public class Address : IValidatable
  {
    public Address();
    public string City { get; set; }
    public CountryCode? Country { get; set; }
    public StateCode? StateCode { get; set; }
    public string StreetAddress1 { get; set; }
    public string StreetAddress2 { get; set; }
    public string ZipCode { get; set; }
  }

您确定 AddressIssuer 是枚举吗?他们对我来说听起来像 类。如果 enumType 不是枚举类型,Enum.Parse(Type enumType, string value) 会抛出异常。

根据您的代码,您的 原始 地址数据位于 FormCollection 中,这是 NameValueCollection 的某种形式。所以你需要知道 formCollection["BillingAddress"] 的内容是什么样的。

如果这是某种形式的 json 或映射,那么您可以使用 Json 解析器或通过反射来解析它并填充您的 Address class。

注意:如果您向我们展示了此密钥的值是什么样的,这将很有帮助,以防这不能回答您的问题。

我已经通过这样做解决了我的问题...

 BillingAddress =
    {
     StreetAddress1 = formCollection["StreetAddress1"],
     StreetAddress2 = formCollection["StreetAddress2"],
     City = formCollection["City"],
     StateCode = (StateCode)Enum.Parse(typeof(StateCode), formCollection["StateCode"]),
     Country = (CountryCode)Enum.Parse(typeof(CountryCode), formCollection["Country"]),
     ZipCode = formCollection["ZipCode"]
    }