当我想在Detail View.cs中显示有关书籍的详细信息时,我会收到此类消息 System.NullReferenceException:'对象引用未设置为对象的实例。' LibaryData.Models.LibaryAsset.Status.get返回null。
我重新填写了应用程序,似乎在我的模型名为LibaryAssets.cs的字段中调用Status有一个数据类型状态
[Required]
public Status Status { get; set; }
到目前为止我的代码:
LibaryAsset.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.DataAnnotations;
namespace LibaryData.Models
{
public abstract class LibaryAsset
{
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public int Year { get; set; }
[Required]
public Status Status { get; set; }
[Required]
public decimal Cost { get; set; }
public int NumberOfCopies { get; set; }
public virtual LibaryBranch Location { get; set; }
public string ImageUrl { get; set; }
}
}
**CatalogController.cs**
using LibaryData;
using LibaryData.Models;
using LibaryManagmentSystems.Models.Catalog;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace LibaryManagmentSystems.Controllers
{
public class CatalogController : Controller
{
private ILibaryAsset _assets;
private ICheckout _checkouts;
public CatalogController(ILibaryAsset assets, ICheckout checkout)
{
_assets = assets;
_checkouts = checkout;
}
public IActionResult Index()
{
var assetModels = _assets.GetAll();
var listingResult = assetModels
.Select(result => new AssetIndexListingModel
{
Id =result.Id,
ImageUrl = result.ImageUrl,
AuthorOrDirector = _assets.GetAuthorOrDirector(result.Id),
DeweyCallNumber = _assets.GetDeweyIndex(result.Id),
Title = result.Title,
Type = _assets.GetType(result.Id)
});
var model = new AssetIndexModel()
{
Assets = listingResult
};
return View(model);
}
public IActionResult Detail(int id)
{
var asset = _assets.GetById(id);
var currentHolds = _checkouts.GetCurrentHolds(id)
.Select(a => new AssetHoldModel
{
HoldPlace = _checkouts.GetCurrentHoldPlaced(a.Id).ToString("d"),
PatronName = _checkouts.GetCurrentHoldPatronName(a.Id)
});
var model = new AssetDetailModel
{
AssetID = id,
Title = asset.Title,
Type = _assets.GetType(id),
Year = asset.Year,
Cost = asset.Cost,
Status = asset.Status.Name,
ImageURL = asset.ImageUrl,
AuthorOrDirector = _assets.GetAuthorOrDirector(id),
CurrentLocation = _assets.GetCurrentLocation(id).Name,
DeweyCallNumber = _assets.GetDeweyIndex(id),
CheckoutHistory = _checkouts.GetCheckOutHistory(id),
ISBN = _assets.GetIsbn(id),
LatestChechout = _checkouts.GetLatesCheckout(id),
PatronName = _checkouts.GetCurrentCheckoutPatron(id),
CurrentHolds = currentHolds
};
return View(model);
}
}
}
所以我在public IActionResult Detail(int id)
var model = new AssetDetailModel
{
AssetID = id,
Title = asset.Title,
Type = _assets.GetType(id),
Year = asset.Year,
Cost = asset.Cost,
Status = asset.Status.Name,
ImageURL = asset.ImageUrl,
AuthorOrDirector = _assets.GetAuthorOrDirector(id),
CurrentLocation = _assets.GetCurrentLocation(id).Name,
DeweyCallNumber = _assets.GetDeweyIndex(id),
CheckoutHistory = _checkouts.GetCheckOutHistory(id),
ISBN = _assets.GetIsbn(id),
LatestChechout = _checkouts.GetLatesCheckout(id),
PatronName = _checkouts.GetCurrentCheckoutPatron(id),
CurrentHolds = currentHolds
};
有任何建议,评论?