【问题标题】:InvalidOperationException Web ApplicationInvalidOperationException Web 应用程序
【发布时间】:2021-05-21 00:15:15
【问题描述】:

我不太熟悉它的工作原理,但我正在尝试提交并计算输入的存款或取款金额,但现在我收到此错误:InvalidOperationException: Nullable object must have a value。

我觉得我需要重新审视这件事,因为我陷入了困境。

BankAppModel.cs:

using System.ComponentModel.DataAnnotations;

namespace Proj1BankApp.Models
{
    public class BankAppModel
    {
        [Required(ErrorMessage = "Please enter a name.")]
        public string Name { get; set; }

        [Required(ErrorMessage = "Please enter a transaction month.")]
        public string TransactionMonth { get; set; }

        [Required(ErrorMessage = "Please enter a transaction day.")]
        [Range(1, 31, ErrorMessage = "The transaction day must be between 1 and 31.")]
        public int TransactionDay { get; set; }

        [Required(ErrorMessage = "Please enter a transaction year.")]
        public int TransactionYear { get; set; }

        public decimal? Balance { get; set; }

        public decimal? WithdrawAmount { get; set; }

        public decimal? DepositAmount { get; set; }

        public decimal Deposit()
        {
            decimal balance = 0;
            balance = Balance.Value + DepositAmount.Value;
            return balance;
        }

        public decimal Withdraw()
        {
            decimal balance = 0;
            if(Balance.Value < WithdrawAmount.Value)
            {
                return balance;
            }
            else
            {
                balance = balance - WithdrawAmount.Value;
                return balance;
            }
        }
    }
}

HomeController.cs:

    using Microsoft.AspNetCore.Mvc;
using Proj1BankApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Proj1BankApp.Controllers
{
    public class HomeController : Controller
    {
        [HttpGet]
        public IActionResult Index()
        {
            ViewBag.BankApp = "";
            return View();
        }

        [HttpPost]
        public IActionResult Index(BankAppModel obj, string submit)
        {
            if (ModelState.IsValid)
            {
                ViewBag.BankApp = obj.Deposit().ToString("c2");
                ViewBag.BankApp = obj.Withdraw().ToString("c2");
            }
            else
            {
                ViewBag.BankApp = "";
            }
            return View();
        }
    }
}

【问题讨论】:

    标签: c# html asp.net-core


    【解决方案1】:

    我假设您的 Deposit 方法内部有 BalanceDepositAmount 空值。尝试在此处放置断点并检查这些属性是否分配了空值。

    或者您可以添加空检查,例如(在 Withdraw 方法中以相同的方式):

    public decimal Deposit()
    {
         decimal balance = 0;
         if(Balance.HasValue && DepositAmount.HasValue)
         {
            balance = Balance.Value + DepositAmount.Value;
         }
         return balance;
    }
    

    【讨论】:

      【解决方案2】:

      请确保您的可空字段在调用其 value 属性之前具有值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-08
        • 2013-05-03
        • 2010-09-21
        • 1970-01-01
        • 1970-01-01
        • 2016-02-17
        • 1970-01-01
        相关资源
        最近更新 更多