这与另一个问题不同。这是更具体的。仅仅知道如何解决null错误将无济于事,因为在这一点上,我无法理解为什么会出现此null错误。据我说,它甚至不应该出现,因为我解决了我在此之前遇到的所有空错误。
我正在做一个银行项目,以教会我更多有关剃刀的知识。我现在想将帐户添加到特定用户,但是即使修复了所有问题,也遇到了空值错误。
最初,空值错误是由于对象状态在ActionResults中不是恒定的,但我使用临时数据解决了此问题。现在我仍然遇到空错误,情况并非如此。
这是银行控制人。它具有从查看用户到添加帐户的所有必要代码
public class BankController : Controller
{
//Global Variables
public static List<UserViewModel> objUsers = new List<UserViewModel>();
public static Vault VaultData = new Vault();
public static BankViewModel BankData = new BankViewModel("Absa", objUsers, VaultData);
public UserViewModel TransferedUser;
string TID;
////Generated Goods
//public static List<Transaction> Transactions;
//public static Savings BCSavings;
//public static Cheque BCCheque;
//public static Credit BCCredit;
//public static List<Account> Accounts;
////Default For New Users
//public List<Transaction> NewTransactions;
//public List<Account> NewAccounts;
//public Savings NewSavings;
//public Cheque NewCheque;
//public Credit NewCredit;
// GET: Bank
public ActionResult Index()
{
return View();
}
public ActionResult Bank()
{
VaultData.Amount = 3500000;
BankData.lsUsers = GenerateTestDataUsers();
return View(BankData);
}
public ActionResult AddUser()
{
return View();
}
[HttpPost]
public ActionResult AddUser(string Name, string Surname)
{
UserViewModel NewUser = new UserViewModel(Name, Surname);
BankData.lsUsers.Add(NewUser);
return View("Bank", new BankViewModel("Absa", BankData.lsUsers, VaultData));
}
[HttpGet]
public ActionResult ViewUser(string id)
{
TransferedUser = BankData.lsUsers.Find(x => x.UserID == id);
//Session["CurrentUser"] = TransferedUser;
TempData["CurrentUser"] = id;
return View(TransferedUser);
}
[HttpPost]
public ActionResult ViewUser(string AACheque = "" , string AASavings = "" , string AACredit = "" )
{
string CurrentUser = TempData["CurrentUser"] as string;
UserViewModel User = BankData.lsUsers.Find(x => x.UserID == CurrentUser);
if (AACheque != "")
{
Cheque NewCheque = new Cheque(Convert.ToDouble(AACheque) , User);
BankData.lsUsers.Find(x => x.UserID == CurrentUser).Accounts.Add(NewCheque); // this is where I get an error when I try to add a new user
}
return View(BankData.lsUsers.Find(x => x.UserID == CurrentUser));
}
public ActionResult AddAccount()
{
return View(TransferedUser);
}
这是银行视图,可在其中查看银行中的用户列表。从这里可以查看用户,以便更改其拥有的帐户或进行交易。
@model _18181539HW05.ViewModels.BankViewModel
@{
ViewBag.Title = "Bank";
}
<head>
<link href="~/StyleSheet1.css" rel="stylesheet" />
</head>
<div id="BankInfo">
<h1>@Model.BankName</h1>
<h2>Vault Total: R @Model.Vault.Amount</h2>
<table>
<tr>
<th colspan="4" id="HeadUser">Users <span>@Html.ActionLink("+", "AddUser", "Bank")</span> </th>
</tr>
<tr>
<th>UserID</th>
<th>Name</th>
<th>Surname</th>
<th></th> @*The Submit Button*@
</tr>
@foreach (var User in Model.lsUsers)
{
<tr>
<td>@User.UserID</td>
<td>@User.UserName</td>
<td>@User.UserSurname</td>
<td>@Html.ActionLink("View User", "ViewUser", new { id = User.UserID }, new { @class = "btn btn-primary" })</td>
</tr>
}
</table>
</div>
这是帐户类,它是抽象类,而Check类是从其派生的。在此示例中,用户具有一个帐户列表,但是一个帐户只能是支票,信用或储蓄。这些类是从Account类继承的,正是这些对象我们添加到用户类的Account List中。
public abstract class Account
{
//Data Members
private Guid mAUID = Guid.NewGuid();
private string mTransactionID;
private string mType;
private double mAmount;
private User mUser;
private UserViewModel UVMUser;
private List<Transaction> mTransactions = new List<Transaction>();
//Constructor(s)
public Account(string Type , double Amount , User objUser )
{
mType = Type;
mAmount = Amount;
mUser = objUser;
}
public Account(string Type, double Amount, UserViewModel objUser)
{
mTransactionID = mAUID.ToString();
mType = Type;
mAmount = Amount;
UVMUser = objUser;
mTransactions = Transactions;
}
public Account()
{
}
//Methods
public string GetAccountType()
{
return mType;
}
public void SetUser(User objUser)
{
mUser = objUser;
}
public virtual void Deposit(double Amount)
{
mAmount = mAmount + Amount;
}
public virtual void Withdraw(double Amount)
{
mAmount = mAmount - Amount;
}
public string GetID()
{
return mTransactionID;
}
public double GetBalance()
{
return mAmount;
}
public void AddTransaction(Transaction NewTransaction)
{
mTransactions.Add(NewTransaction);
}
public virtual void Transfer(double TransferValue)
{
mAmount = mAmount - TransferValue;
}
//Properties
protected string Type
{
get
{
return mType;
}
set
{
mType = value;
}
}
protected double Amount
{
get
{
return mAmount;
}
set
{
mAmount = value;
}
}
protected User objUser
{
get
{
return mUser;
}
set
{
mUser = value;
}
}
protected List<Transaction> Transactions
{
get
{
return mTransactions;
}
set
{
mTransactions = value;
}
}
public string TransactionID
{
get
{
return mTransactionID;
}
set
{
mTransactionID = value;
}
}
public Guid AUID
{
get
{
return mAUID;
}
set
{
mAUID = value;
}
}
这是检查类。我只显示Check类以减少我发布的代码量。如果我们可以成功地将checque类添加到User类的帐户列表中,那么就可以解决我的问题。
public class Cheque : Account
{
//Data Members
double mSaveAmount;
//Constructor(s)
public Cheque(double SaveAmount, User SObjUser) : base("Cheque", 0, SObjUser)
{
mSaveAmount = SaveAmount;
}
public Cheque(double SaveAmount, UserViewModel SObjUser) : base("Cheque", 0, SObjUser)
{
mSaveAmount = SaveAmount;
}
//Methods
public override void Withdraw(double Amount)
{
base.Withdraw(Amount);
}
public double GetSaveAmount()
{
return mSaveAmount;
}
//Properties
public double MSaveAmount
{
get
{
return mSaveAmount;
}
set
{
mSaveAmount = value;
}
}
}
这是视图用户VIEW,我们可以在其中向该用户添加帐户。所有这些视图均由银行控制器处理
@model _18181539HW05.ViewModels.UserViewModel
@{
ViewBag.Title = "ViewUser";
}
<head>
<link href="~/StyleSheet1.css" rel="stylesheet" />
</head>
<h2>View User</h2>
<div>
<p>Name : @Model.UserName </p>
<br />
<p>Surname : @Model.UserSurname</p>
<table>
<tr>
<th colspan="6">Accounts <span>@Html.ActionLink("Transfer", "Transfer", "Bank", new { @class = "btn btn-primary" })</span> <span> @Html.ActionLink("+", "AddAccount", "Bank", new { @class = "btn btn-primary" }) </span> </th>
</tr>
<tr>
<th>Number</th>
<th>Type</th>
<th>Balance</th>
<th></th>
<th></th>
<th></th>
</tr>
@{ int Counter = 0;}
@{ if (Model.Accounts != null)
{
foreach (var Account in Model.Accounts)
{
if (Account != null)
{
<tr>
<td>@{ Counter++; }</td>
<td>@Account.GetAccountType()</td>
<td>@Account.GetBalance()</td>
<td>@Html.ActionLink("Deposit", "DepositWithdraw", "Bank", new { @class = "btn btn-primary" })</td>
<td>@Html.ActionLink("Withdraw", "DepositWithdraw", "Bank", new { @class = "btn btn-primary" })</td>
<td>@Html.ActionLink("View", "ViewAccount", "Bank", new { @class = "btn btn-primary" })</td>
</tr>
} /*end of if*/
}
}
}
</table>
</div>
这是银行类。在银行类中,有一个用户列表。这是我们将要更改的信息。我的目的是向用户列表中的单个用户实例添加一个帐户
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace _18181539HW05.Models
{
public class Bank
{
// Data Members
private string mBankName;
private List<User> mlsUsers;
private Vault mVault;
private List<Account> mAccounts;
private List<Transaction> mTransactions;
//Constructor(s)
public Bank(string BankName, List<User> lstUser, Vault Vault)
{
mBankName = BankName;
mlsUsers = lstUser;
mVault = Vault;
}
//Methods
public string GetName()
{
return mBankName;
}
public List<Account> GetAccounts()
{
return mAccounts;
}
public Account GetAccountByID(Guid Id)
{
foreach (Account SingleAccount in mAccounts)
{
if (SingleAccount.AUID == Id)
{
return SingleAccount;
}
}
return null;
}
public void OpenAccount(Account NewAccount , string Id)
{
foreach (var User in mlsUsers)
{
if (User.UserID == Id)
{
NewAccount.SetUser(User);
mAccounts.Add(NewAccount);
}
}
}
public Vault GetVault()
{
return mVault;
}
public void DepositIntoAccount(double Amount)
{
}
//Properties
public string BankName
{
get
{
return mBankName;
}
set
{
mBankName = value;
}
}
public List<User> lsUsers
{
get
{
return mlsUsers;
}
set
{
mlsUsers = value;
}
}
public Vault Vault
{
get
{
return mVault;
}
set
{
mVault = value;
}
}
public List<Account> Accounts
{
get
{
return mAccounts;
}
set
{
mAccounts = value;
}
}
public List<Transaction> Transactions
{
get
{
return mTransactions;
}
set
{
mTransactions = value;
}
}
}
}
这是我添加帐户的视图,目前只有支票有效,因为如果我能解决它,那么我就可以处理其余的
@using _18181539HW05.ViewModels
@model UserViewModel
@{
ViewBag.Title = "AddAccount";
}
<head>
<link href="~/StyleSheet1.css" rel="stylesheet" />
</head>
<div id="AddAccount">
<h2>Add Account</h2>
@using (Html.BeginForm("ViewUser" , "Bank" , FormMethod.Post))
{
<label for="AccountType">Account Type:</label>
<select id="AccountType" name="AccountType" class="form-control">
<option>Select an Account type to add</option>
<option>Cheque</option>
<option>Savings</option>
<option>Credit</option>
</select>
<br />
<label for="Cheque" id="Cheque" style="display:none">Savings Amount:</label>
<input type="text" id="ChqTxtBox" name="AACheque" style="display:none" class="form-control , Cheque" />
<label for="Savings" id="Savings" style="display:none">Interest Amount:</label>
<input type="text" id="SavTxtBox" name="AASavings" style="display:none" class="form-control" />
<label for="Credit" id="Credit" style="display:none">Credit Limit:</label>
<input type="text" id="CreTxtBox" name="AACredit" style="display:none" class="form-control" />
<br />
<br />
<input type="submit" value="Add" class="btn btn-primary" />
<input type="submit" value="Cancel" class="btn btn-primary" />
}
</div>
<script src="~/Scripts/Hide-Show.js"></script>
这是我在“添加帐户”视图中使用的Java脚本
//alert("Just checking if its working!")
var DropDown = document.getElementById("AccountType")
DropDown.onchange = function show()
{
if (DropDown.value == "Cheque")
{
document.getElementById("ChqTxtBox").style.display = "inline-block"
document.getElementById("Cheque").style.display = "inline-block"
}
else
{
document.getElementById("ChqTxtBox").style.display = "none"
document.getElementById("Cheque").style.display = "none"
}
if (DropDown.value == "Savings") {
document.getElementById("SavTxtBox").style.display = "inline-block"
document.getElementById("Savings").style.display = "inline-block"
}
else {
document.getElementById("SavTxtBox").style.display = "none"
document.getElementById("Savings").style.display = "none"
}
if (DropDown.value == "Credit") {
document.getElementById("CreTxtBox").style.display = "inline-block"
document.getElementById("Credit").style.display = "inline-block"
}
else {
document.getElementById("CreTxtBox").style.display = "none"
document.getElementById("Credit").style.display = "none"
}
}
最后这是用户类。我将其包含在代码中,因为银行有一个用户列表,我们要在其中将支票帐户添加到“帐户”列表中
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace _18181539HW05.Models
{
public class User
{
//GUID
Guid UUID = Guid.NewGuid();
//Data Members
private string mUserName;
private string mUserSurname;
private string mUserID;
private List<Account> mAccounts;
private List<Transaction> mTransactions;
//Constructor
public User(string Name, string Surname )
{
mUserName = Name;
mUserSurname = Surname;
mUserID = UUID.ToString();
}
//Methods
public List<Account> GetAccounts()
{
return mAccounts;
}
public Account GetAccountByID(Guid Id)
{
foreach (Account SingleAccount in mAccounts)
{
if (SingleAccount.AUID == Id)
{
return SingleAccount;
}
}
return null;
}
public void AddAccount(Account NewAccount)
{
}
//Properties
public string UserName
{
get
{
return mUserName;
}
set
{
mUserName = value;
}
}
public string UserSurname
{
get
{
return mUserSurname;
}
set
{
mUserSurname = value;
}
}
public string UserID
{
get
{
return mUserID;
}
set
{
mUserID = value;
}
}
public List<Account> Accounts
{
get
{
return mAccounts;
}
set
{
mAccounts = value;
}
}
public List<Transaction> Transactions
{
get
{
return mTransactions;
}
set
{
mTransactions = value;
}
}
}
}
我希望将一个帐户添加到“用户”类的“帐户列表”中,该列表可在“银行”类的用户列表中找到。但这不会发生,因为即使我以为我已经解决了所有这些问题,但仍然出现空错误。