Type checking in Razor
I have public IList<ListingHistoryItem> History { get; set; }
in one of my pages' view models. The ListingHistoryItem
is an abstract class and has two subclasses BidDto
and CommentDto
. The reason why I'm doing it this way is because, the listing has some 开发者_高级运维sort of a news feed which displays any bids or comments on the listing.
So anyway, what I'm trying to do is, in my view check the concrete type of the History element and then display it accordingly. Here's how I'm trying to do it:
@for (int i = 0; i < Model.History.Count; i++)
{
var feed = Model.History[i];
if (feed.GetType().FullName.Equals(Sharwe.MVC.Models.BidDto))
But this last line gives the following exception:
Sharwe.MVC.Models.BidDto' is a 'type', which is not valid in the given context
So how am I supposed to check for the type? Or am I doing it totally wrong here?
Try like this:
if (feed is Sharwe.MVC.Models.BidDto)
{
...
}
精彩评论