I'm not very familiar with how this works, but I'm trying to submit and calculate the entered amounts for either deposit or withdraw, but now I'm getting this error: InvalidOperationException: Nullable object must have a value.
I feel like I need a fresh set of eyes on this because I'm mentally stuck.
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();
}
}
}
