【发布时间】: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