【问题标题】:Blazor application can't find nested modelBlazor 应用程序找不到嵌套模型
【发布时间】:2021-01-04 15:18:56
【问题描述】:

我有这个 Blazor 应用程序,其中有以下模型类:

    public class Purchase
{
    public Payment payment { get; set; }
}

public class Payment
{
    public string operation { get; set; }
    public string intent { get; set; }
    public string currency { get; set; }
    public Price[] prices { get; set; }
    public string description { get; set; }
    public string userAgent { get; set; }
    public string language { get; set; }
    public Urls urls { get; set; }
    public Payeeinfo payeeInfo { get; set; }
    public Cardholder cardholder { get; set; }
    public Riskindicator riskIndicator { get; set; }
}

public class Urls
{
    public string completeUrl { get; set; }
    public string cancelUrl { get; set; }
    public string callbackUrl { get; set; }
    public string logoUrl { get; set; }
    public string termsOfServiceUrl { get; set; }
}

public class Payeeinfo
{
    public string payeeId { get; set; }
    public string payeeReference { get; set; }
    public string payeeName { get; set; }
    public string productCategory { get; set; }
    public string orderReference { get; set; }
    public string subsite { get; set; }
}

public class Cardholder
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string email { get; set; }
    public string msisdn { get; set; }
    public string homePhoneNumber { get; set; }
    public string workPhoneNumber { get; set; }
    public Shippingaddress shippingAddress { get; set; }
}

public class Shippingaddress
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public string email { get; set; }
    public string msisdn { get; set; }
    public string streetAddress { get; set; }
    public string coAddress { get; set; }
    public string city { get; set; }
    public string zipCode { get; set; }
    public string countryCode { get; set; }
}

public class Riskindicator
{
    public string deliveryEmailAddress { get; set; }
    public string deliveryTimeFrameIndicator { get; set; }
    public string preOrderDate { get; set; }
    public string preOrderPurchaseIndicator { get; set; }
    public string shipIndicator { get; set; }
    public bool giftCardPurchase { get; set; }
    public string reOrderPurchaseIndicator { get; set; }
    public Pickupaddress pickUpAddress { get; set; }
}

public class Pickupaddress
{
    public string name { get; set; }
    public string streetAddress { get; set; }
    public string coAddress { get; set; }
    public string city { get; set; }
    public string zipCode { get; set; }
    public string countryCode { get; set; }
}

public class Price
{
    public string type { get; set; }
    public int amount { get; set; }
    public int vatAmount { get; set; }
}

我尝试在 razor 组件中这样实现:

@page "/testpay"
@using Kanal10.UI.Models.Payex

<!-- Content -->
<div class="donate">
    <div class="donate-pay wrapper">
        <EditForm Model="purchase" OnValidSubmit="SavePurchase">
            <div class="row">
                <div class="col-md-6 col-sm-12">
                    <div class="form-group">
                        <label>Firstname</label>
                        <InputText id="firstName" @bind-value="purchase.payment.cardholder.firstName" class="form-control"></InputText>
                    </div>
                </div>

            </div>
            <button type="submit" class="btn btn-primary">Save</button>
        </EditForm>


    </div>
</div>
@code {
    private Purchase purchase = new Purchase();

    private void SavePurchase()
    {

    }
}

但是当我渲染页面时,我收到错误消息,我不太明白为什么。

NullReferenceException:对象引用未设置为对象的实例。 Kanal10.UI.Pages.PayTest.b__0_1(RenderTreeBuilder __builder2)

如果有人有答案,我将不胜感激。

彼得

【问题讨论】:

    标签: asp.net-core razor blazor


    【解决方案1】:

    为了做这样的事情:

    <InputText id="firstName" @bind-value="purchase.payment.cardholder.firstName" class="form-control"></InputText>
    

    你必须这样做:

    @code
    {
        private Purchase purchase = new Purchase { payment=new Payment() 
                                        {cardholder=new Cardholder(){}}};
    }
    

    如果我没有错过链中的某些内容,这应该可以工作。但是原则很清楚:您必须实例化一个嵌入在父对象实例中的对象,该对象本身嵌入在祖父对象的实例中,依此类推。这与 Blazor 无关。这是 C#。您必须先实例化一个对象才能使用它,不是吗?

    注意:字段的名称,例如payment in

    public Payment payment { get; set; }
    

    不仅可以命名为Payment,而且按照惯例我们是这样定义的(尤其是以大写字母开头)。所以让它public Payment Payment{ get; set; }

    【讨论】:

    • 超级,这解释了它。谢谢:)
    【解决方案2】:

    【讨论】:

    • 我这样初始化:protected override void OnInitialized() { purchase = new Purchase();付款=新付款();持卡人 = 新持卡人(); } 还是一样的错误
    • 尝试将这些作为默认值添加到您的模型字段中
    猜你喜欢
    • 2021-11-08
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多