我开始使用ASP.NET MVC Core进行编码,我想为库系统中的Checkout书籍创建一个新模型。
我创建了一个名为CheckoutModel的新子文件夹,在其中我创建了CheckoutModel.cs
当我运行应用程序时,它给我发了一个告诉我的错误:
无法隐式转换类型' LibaryData.Models.Checkout'至 ' LibaryManagmentSystems.Models.Checkout.CheckoutModel'
我尝试重命名类名并更改子文件夹名称和模型名称,但同样问题。
CatalogController.cs
using LibaryData;
using LibaryManagmentSystems.Models.Catalog;
using LibaryManagmentSystems.Models.Checkout;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using LibaryData.Models;
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 Checkout(int id)
{
var asset = _assets.GetById(id);
var model = new Models.Checkout.CheckoutModel
{
AssetId = id,
ImageUrl = asset.ImageUrl,
Title = asset.Title,
LibaryCardId ="",
isCheckOut = _checkouts.IsCheckedOut(id)
};
return View(model);
}
public IActionResult MarkLost(int assetID)
{
_checkouts.MarkLost(assetID);
return RedirectToAction("Detail", new { id = assetID });
}
[HttpPost]
public IActionResult PlaceCheckout(int assetID, int libaryCardId)
{
_checkouts.CheckInItem(assetID, libaryCardId);
return RedirectToAction("Detail", new { id = assetID });
}
[HttpPost]
public IActionResult PlaceHold(int assetID, int libaryCardId)
{
_checkouts.PlaceHold(assetID, libaryCardId);
return RedirectToAction("Detail", new { id = assetID });
}
public IActionResult Hold(int id)
{
var asset = _assets.GetById(id);
var model = new CheckoutModel
{
AssetId = id,
ImageUrl = asset.ImageUrl,
Title = asset.Title,
LibaryCardId = "",
isCheckOut = _checkouts.IsCheckedOut(id),
HoldCount = _checkouts.GetCurrentHolds(id).Count()
};
return View(model);
}
}
}
Checkout.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.DataAnnotations;
namespace LibaryData.Models
{
public class Checkout
{
public int Id { get; set; }
[Required]
public LibaryAsset LibaryAsset { get; set; }
public LibaryCard LibaryCard { get; set; }
public DateTime Since { get; set; }
public DateTime Until { get; set; }
}
}
CheckoutModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace LibaryManagmentSystems.Models.Checkout
{
public class CheckoutModel
{
public string LibaryCardId { get; set; }
public string Title { get; set; }
public int AssetId { get; set; }
public string ImageUrl { get; set; }
public int HoldCount { get; set; }
public bool isCheckOut { get; set; }
}
}
任何建议,有帮助吗?
它在CatalogController.cs中显示错误:
LatestChechout = _checkouts.GetLatesCheckout(id)
public Checkout GetLatesCheckout(int assetId)
{
return _context.Checkouts
.Where(c => c.LibaryAsset.Id == assetId)
.OrderByDescending(c=>c.Since)
.FirstOrDefault();
}
AssetsDetailModel.cs
using LibaryData.Models;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using LibaryData.Models;
using System;
namespace LibaryManagmentSystems.Models.Catalog
{
public class AssetDetailModel
{
public int AssetID { get; set; }
public string Title { get; set; }
public string AuthorOrDirector { get; set; }
public string Type { get; set; }
public int Year { get; set; }
public string ISBN { get; set; }
public string DeweyCallNumber { get; set; }
public string Status { get; set; }
public decimal Cost { get; set; }
public string CurrentLocation { get; set; }
public string ImageURL { get; set; }
public string PatronName { get; set; }
public Checkout LatestCheckout { get; set; }
public IEnumerable<CheckoutHistory> CheckoutHistory { get; set; }
public IEnumerable<AssetHoldModel> CurrentHolds { get; set; }
}
public class AssetHoldModel
{
public string PatronName { get; set; }
public string HoldPlace { get; set; }
}
}
答案 0 :(得分:0)
根据第二个错误(在评论中)&#34;数据类型&#39; Checkout&#39;是一个名称空间但是像LibaryManagmentSystems类型一样使用,代码在名为Checkout
的类和名为Checkout
的名称空间之间混淆,这些名称都可以从AssetDetailModel
访问(由于类所在的命名空间以及文件中的using
语句。
要解决此问题,您可以
1)更改您的类或命名空间,使它们不再具有相同的名称
或者
2)通过在属性声明中显式指定checkout类的完整命名空间来消除代码中的歧义:
public LibaryData.Models.Checkout LatestCheckout { get; set; }
这将清楚地表明它正是被引用的对象(而不是命名空间LibaryManagmentSystems.Models.Checkout
)。