从我之前的帖子开始,我正在为一个大学项目制作一个网络ATM。为此,我有一个MVC应用程序,其中大部分功能都是基于它,以及一个API项目,其中包含一个数据库。我正在尝试使用此数据库中的数据并将其显示在我的索引方法中。但是,当我运行我的解决方案时,我在索引视图的以下行给出了一个空对象引用:
@foreach (var item in Model.ExcRateList)
我正在努力弄清楚为什么这不起作用,并且正在向你们寻求可能的解决方案。
以下是我的控制器中的索引获取方法:
// GET: AtmAccounts
public ActionResult Index()
{
ViewModels.ExcClient ex = new ViewModels.ExcClient();
ViewBag.listRates = ex.findAll();
var userID = User.Identity.GetUserId();
ViewModels.AtmAccountVM atmVm = new ViewModels.AtmAccountVM();
atmVm.AtmAccountList = db.AtmAccounts.Where(a => a.UserId == userID).ToList();
atmVm.UserName = User.Identity.GetUserName();
atmVm.ExcRateList = ViewBag.listRates;
return View(atmVm);
}
涉及此问题的部分是:
ViewModels.ExcClient ex = new ViewModels.ExcClient();
ViewBag.listRates = ex.findAll();
和
atmVm.ExcRateList = ViewBag.listRates;
视图模型AtmAccountVM控制我的不同方法之间传递的值,包括我的API(或者至少应该):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ATMApplication.Models;
using System.Data.SqlClient;
namespace ATMApplication.ViewModels
{
public class AtmAccountVM
{
public List<AtmAccount> AtmAccountList { get; set; }
public List<Transaction> TransactionsList { get; set; }
public string UserName { get; set; }
public int Id { get; set; }
//These variables are used for the various ATM functions accessible to customers
public decimal DepositAmount { get; set; }
public decimal WithdrawAmount { get; set; }
public decimal TransferAmount { get; set; }
public string AccTransfer { get; set; }
// These are Used for the Restful Service
public int id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public decimal Rate { get; set; }
public List<ExcRate> ExcRateList { get; set; }
}
}
以下是视图模型ExcRate,它存储从API检索的数据,以及获取它的ExcClient:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ATMApplication.ViewModels
{
public class ExcRate
{
public int id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public decimal Rate { get; set;}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Newtonsoft.Json;
namespace ATMApplication.ViewModels
{
public class ExcClient
{
private string Base_Url = "http://localhost.57341/api/values";
public IEnumerable<ExcRate> findAll()
{
try
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(Base_Url);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("rates").Result;
if (response.IsSuccessStatusCode)
{
var responseData = response.Content.ReadAsStringAsync().Result;
var Rates = JsonConvert.DeserializeObject<List<ExcRate>>(responseData);
return Rates;
}
return null;
}
catch
{
return null;
}
}
}
}
最后,这里是索引视图,其底部部分用于在表格中显示此数据:
@using ATMApplication.ViewModels
@model ATMApplication.ViewModels.AtmAccountVM
<h2> Accounts Held for Customer: @Model.UserName </h2>
<p>
@Html.ActionLink("Create New Account", "Create")
</p>
<style typw ="text/css">
span {
color: darkcyan;
}
</style>
<div style="background-color:lightyellow; color: black;">
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.AtmAccountList.First().AccountNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.AtmAccountList.First().AccountBalance)
</th>
<th>
@Html.DisplayNameFor(model => model.AtmAccountList.First().AccType.AccountType)
</th>
<th></th>
</tr>
@foreach (var item in Model.AtmAccountList)
{
<tr>
<td>
<div style="background-color:lightyellow">
@Html.DisplayFor(modelItem => item.AccountNumber)
</div>
</td>
<td>
<div style="background-color:lightyellow">
@Html.DisplayFor(modelItem => item.AccountBalance)
</div>
</td>
<td>
<div style="background-color:lightyellow">
@Html.DisplayFor(modelItem => item.AccType.AccountType)
</div>
</td>
<td>
<div style="background-color:lightyellow">
@Html.ActionLink("Deposit", "Deposit", new { id = item.Id })
</div>
<div style="background-color:lightyellow">
@Html.ActionLink("Quick Withdraw", "Quick Withdraw", new { id = item.Id })
</div>
<div style="background-color: lightyellow">
@Html.ActionLink("Withdraw", "Withdraw", new { id = item.Id })
</div>
<div style="background-color:lightyellow">
@Html.ActionLink("Transfer", "Transfer", new { id = item.Id })
</div>
<div style="background-color:lightyellow">
@Html.ActionLink("Transactions", "Transactions", new { id = item.Id })
<input type="hidden" name="Id" value="@Model.AtmAccountList[0].Id" />
</div>
</td>
</tr>
}
</table>
</div>
<br />
<br />
<h2>Exchange Rates</h2>
<p>
These are the current exchange rates (in GBP):
</p>
<br/>
<div style="background-color:lightcyan">
<table class="table">
<tr>
<th>Currency Code</th>
<th>Currency Name</th>
<th>Exchange Rate (GBP)</th>
</tr>
@foreach (var item in Model.ExcRateList)
{
<tr>
<td>@item.Code</td>
<td>@item.Name</td>
<td>@item.Rate</td>
</tr>
}
</table>
</div>