开发者

Type checking in Razor

开发者 https://www.devze.com 2023-02-26 00:12 出处:网络
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 reas

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)
{
    ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消