我有public IList<ListingHistoryItem> History { get; set; }
在我的一个页面的 View 模型中。 ListingHistoryItem
是一个抽象类,有两个子类BidDto
和 CommentDto
.我这样做的原因是,该列表有某种新闻提要,可以显示对该列表的任何出价或评论。
所以无论如何,我想做的是,在我看来,检查 History 元素的具体类型,然后相应地显示它。以下是我正在努力做到的:
@for (int i = 0; i < Model.History.Count; i++)
{
var feed = Model.History[i];
if (feed.GetType().FullName.Equals(Sharwe.MVC.Models.BidDto))
但是最后一行给出了以下异常:
Sharwe.MVC.Models.BidDto' is a 'type', which is not valid in the given context
那么我应该如何检查类型呢?还是我在这里完全错了?
最佳答案
像这样尝试:
if (feed is Sharwe.MVC.Models.BidDto)
{
...
}
关于c# - Razor 中的类型检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5695223/