我有3个依赖模型和我的项目
public class EditPaymentMethodModel
{
public PaymentMethodViewData PaymentViewData { get; set; }
public BillingAddressModel BillingAddresses { get; set; }
public CspacModel NewBillingAddress { get; set; }
}
public class PaymentMethodViewData
{
public int? PaymentMethodId { get; set; }
}
public class CreditCardViewData : PaymentMethodViewData
{
public string CardNumber { get; set; }
....
....
....
[ViagogoCpfCnpj(CardNoProperty = "CardNumber")]
public string CpfOrCnpj { get; set; }
}
PaymentMethodViewData实际上没有任何CreditCardViewData,但我们有付款方式处理程序,并且在此行之后将creditcardviewdata添加到paymentmethodviewdata
var paymentMethodViewData = PaymentMethodHandler.GetPaymentMethodData(payment, userId);
最后我有一个视图,它的模型是EditPaymentMethodModel
@model Viagogo.Mvc.OM.Pipeline.BuyPipeline.EditPaymentMethodModel
我想要做的是,如果付款类型是信用卡,我只想做的就是下面一行,但是目前它给出了一个错误,我无法弄清楚
@Html.TextBoxFor(m => m.PaymentViewData.CreditCardViewData.CpfOrCnpj)
编辑1:错误在运行时
答案 0 :(得分:1)
如果您无法将public PaymentMethodViewData PaymentViewData { get; set; }
更改为public CreditCardViewData PaymentViewData { get; set; }
,那么您可以在视图模型中添加其他属性 - 例如
public class EditPaymentMethodModel
{
....
[ViagogoCpfCnpj(CardNoProperty = "CardNumber")]
public string CpfOrCnpj
{
get
{
if (PaymentViewData != null && PaymentViewData is CreditCardViewData)
{
return ((CreditCardViewData)PaymentViewData).CpfOrCnpj;
}
return null;
}
}
}
您可以在视图中使用
进行访问@Html.TextBoxFor(m => m.PaymentViewData.CreditCardViewData.CpfOrCnpj)
但是此属性没有setter,因此ModelBinder不会为CpfOrCnpj
设置任何值。如果您在post方法中需要此值,则需要从FormCollection