C# 无法声明模型";无法隐式转换类型";
我对使用ASP.NETMVC核心进行编码还不熟悉,我想为图书馆系统中的图书结账创建一个新模型。 我创建了一个名为CheckoutModel的新子文件夹,并在其中创建了C# 无法声明模型";无法隐式转换类型";,c#,asp.net-mvc,asp.net-core,C#,Asp.net Mvc,Asp.net Core,我对使用ASP.NETMVC核心进行编码还不熟悉,我想为图书馆系统中的图书结账创建一个新模型。 我创建了一个名为CheckoutModel的新子文件夹,并在其中创建了CheckoutModel.cs 当我运行应用程序时,它会给我一个错误,告诉我: 无法将类型“LibaryData.Models.Checkout”隐式转换为 '库管理系统.模型.签出.签出模型' 我试图重命名类名并更改子文件夹名和模型名,但还是出现了同样的问题 CatalogController.cs using LibaryDa
CheckoutModel.cs
当我运行应用程序时,它会给我一个错误,告诉我:
无法将类型“LibaryData.Models.Checkout”隐式转换为
'库管理系统.模型.签出.签出模型'
我试图重命名类名并更改子文件夹名和模型名,但还是出现了同样的问题
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);
}
}
}
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; }
}
}
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; }
}
}
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; }
}
}
Checkout.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);
}
}
}
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; }
}
}
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; }
}
}
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; }
}
}
签出模型.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);
}
}
}
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; }
}
}
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; }
}
}
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; }
}
}
有什么建议或帮助吗
它在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;
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);
}
}
}
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; }
}
}
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; }
}
}
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; }
}
}
使用LibaryData.Models;
使用System.Collections.Generic;
使用System.ComponentModel.DataAnnotations;
使用LibaryData.Models;
使用制度;
命名空间libarymanagementsystems.Models.Catalog
{
公共类AssetDetailModel
{
公共int AssetID{get;set;}
公共字符串标题{get;set;}
公共字符串AuthorOrDirector{get;set;}
公共字符串类型{get;set;}
公共整数年{get;set;}
公共字符串ISBN{get;set;}
公共字符串DeweyCallNumber{get;set;}
公共字符串状态{get;set;}
公共十进制成本{get;set;}
公共字符串CurrentLocation{get;set;}
公共字符串ImageURL{get;set;}
公共字符串名称{get;set;}
公共签出LatestCheckout{get;set;}
公共IEnumerable签出历史记录{get;set;}
公共IEnumerable CurrentHolds{get;set;}
}
公共类资产模型
{
公共字符串名称{get;set;}
公共字符串保持位置{get;set;}
}
}
根据第二个错误(在注释中)“数据类型‘Checkout’是一个名称空间,但与libarymanagementsystems类型一样使用”,代码在名为Checkout
的类和名为Checkout
的名称空间之间混淆,这两个名称空间都可以从AssetDetailModel
访问(由于类所在的名称空间,以及文件中的using
语句)
要解决此问题,您可以
1) 更改类或命名空间,使它们不再具有相同的名称
或
2) 通过在属性声明中显式指定签出类的完整命名空间,消除代码中的歧义:
public LibaryData.Models.Checkout LatestCheckout { get; set; }
这将清楚地表明正是引用的对象(而不是名称空间
libarymanagementsystems.Models.Checkout
)。您实际上没有直接提供相关代码,但似乎\u Checkout.GetLatesCheckout(id)
返回一种类型的对象(Checkout),但是LatestChechout
属性被声明为另一种不同类型的对象(CheckoutModel)。您不能只是随机地将一种类型交换为另一种类型。我为GetLatesCheckoutOk添加了方法,以便返回类型为Checkout
的对象,我猜AssetDetailModel.LatestCheckOut
的声明是公共CheckoutModel LatestCheckOut
?就像我说的,它们是两个完全不同的物体。你不能指望一个能神奇地变成另一个。代码应该如何知道如何做到这一点?它们是完全不相关的类。就像你告诉电脑把汽车变成兔子什么的,却没有给出任何操作说明。您需要在语句的两边使用相同的类型,或者编写一个转换例程。哪一行抛出了错误?谢谢@ADyson请将此作为答案发布,我可以投赞成票。你帮了我很多:)谢谢:)