我是 asp.net 的新手。我面临的问题是我无法通过预订按钮进行预订。所有数据都以天蓝色存储在表存储中,不为空值。但我得到的问题是 NullReferenceException: Object reference not set to an instance of an object。 bookdata.cshtml 中的 AspNetCore.Views_Table_bookdata.ExecuteAsync() 代码行:59 @{ var displayresult = Model.Result as ddacassignment.Models.ServicesEntity;} 有谁知道如何解决这个问题?谢谢
服务实体文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using microsoft.WindowsAzure.Storage.Table;
namespace ddacassignment.Models
{
public class ServicesEntity : TableEntity
{
public ServicesEntity() { }
public ServicesEntity(String service,string company)
{
this.PartitionKey = service;
this.RowKey = company;
}
public DateTime Schedule { get; set; }
public double Price { get; set; }
public string customerusername { get; set; }
public bool isBooked { get; set; }
public bool isConfirmed { get; set; }
}
}
Table Controller(书籍数据)方法
public actionResult bookdata(string PartitionKey,string RowKey)
{
CloudTable table = getTableStorageInformation();
string errormessage = null;
//get current username
var myusername = this.userManager.Getusername(HttpContext.User);
try
{
TableOperation retrieveOperation = TableOperation.Retrieve<ServicesEntity>(PartitionKey,RowKey);
//Execute the operation
TableResult result = table.ExecuteAsync(retrieveOperation).Result;
//asign the result to item objct
ServicesEntity updateEntity = (ServicesEntity)result.Result;
//change the description
updateEntity.isBooked = true;
updateEntity.customerusername = myusername;
//create the inssertorreplace tableoperation
TableOperation insertorReplaceOperation = TableOperation.InsertOrReplace(updateEntity);
//execute the operation
TableResult resultof = table.ExecuteAsync(insertorReplaceOperation).Result;
ViewBag.Result = result.HttpStatusCode;
return View(resultof);
}
catch (Exception ex)
{
ViewBag.msg = "Technical Error: " + ex.ToString();
}
ViewBag.msg = errormessage;
return View();
}
查看代码
@model microsoft.WindowsAzure.Storage.Table.TableResult
@{ ViewData["Title"] = "Bookdata"; }
<style>
body {
font-family: 'lato',sans-serif;
}
.container {
max-width: 1000px;
margin-left: auto;
margin-right: auto;
padding-left: 10px;
padding-right: 10px;
}
th {
background-color: #95A5A6;
font-size: 14px;
text-transform: uppercase;
letter-spacing: 0.03em;
padding: 20px;
}
tr {
background-color: #ffffff;
box-shadow: 0px 0px 9px 0px rgba(0,0.1);
}
td {
padding: 20px;
}
</style>
<h1> Summary of Booked Slot</h1>
@if (ViewBag.Message != null)
{
<p>@ViewBag.Message</p> }
else
{
<table border="1">
<tr>
<th style="width:15%">Service Name</th>
<th style="width:15%">Company name</th>
<th style="width:15%">username</th>
<th style="width:15%">Schedule</th>
<th style="width:15%">Price</th>
<th style="width:15%">isBooked</th>
<th style="width:15%">Customerusername</th>
<th style="width:15%">isConfirmed</th>
</tr>
<tr>
@{ var displayresult = Model.Result as ddacassignment.Models.ServicesEntity;}
<td style="width:15%">@displayresult.PartitionKey</td>
<td style="width:15%">@displayresult.RowKey</td>
<td style="width:15%">@displayresult.Schedule</td>
<td style="width:15%">@displayresult.Price</td>
<td style="width:15%">@displayresult.isBooked</td>
<td style="width:15%">@displayresult.customerusername</td>
<td style="width:15%">@displayresult.isConfirmed</td>
</tr>
</table>}