【问题标题】:How to solve the error "Type provided must be an Enum. Parameter name: enumType"如何解决错误“提供的类型必须是枚举。参数名称:枚举类型”
【发布时间】:2017-08-01 20:53:38
【问题描述】:

所以我一直在使用 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; }
  }

【问题讨论】:

    标签: c# asp.net-mvc enums formcollection paysimple


    【解决方案1】:

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

     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"]
        }
    

    【讨论】:

      【解决方案2】:

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

      如果这是某种形式的 json 或 map,那么您可以使用 Json 解析器或通过反射对其进行解析并填充您的 Address 类。

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

      【讨论】:

      • 我会添加到答案中
      • 我已经编辑了答案,帐单地址中有值
      【解决方案3】:

      你确定AddressIssuer 是枚举吗?他们听起来像我的课。如果enumType 不是枚举类型,Enum.Parse(Type enumType, string value) 将引发异常。

      【讨论】:

      • public enum Issuer { Visa = 12, Master = 13, Amex = 14, Discover = 15 }
      • 所以我觉得地址不是枚举,
      • @SylzzHeartnet,那时可能就是这样。
      • 正确的称呼方式是什么??
      • 这确实不像枚举。您可以尝试BillingAddress = formCollection["BillingAddress"] 并制作BillingAddress 类型为Address
      猜你喜欢
      • 1970-01-01
      • 2022-07-14
      • 1970-01-01
      • 2013-11-03
      • 2012-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多