Sachin Kainth
Sachin Kainth

Reputation: 46750

WebGrid grid.Columns Format Error

@{
    var grid = new WebGrid(Model.Auctions, rowsPerPage: Model.PagingInfo.ItemsPerPage, defaultSort: "AddedDate");
}

@grid.GetHtml(
        columns: grid.Columns(
                **grid.Column(columnName: "", header: "Type", format: (auction) => AuctionListViewModel.GetAuctionType(auction)),**
                grid.Column(columnName: "OwnerReference", header: "Owner reference")
            )
        );

public class AuctionListViewModel
    {
        public IEnumerable<Auction> Auctions { get; set; }
        public IEnumerable<Item> Items { get; set; }
        public PagingInfo PagingInfo { get; set; }
        public string Title { get; set; }
        public string Action { get; set; }

        public static string GetAuctionType(Auction auction)
        {
            var type = string.Empty;
            if (auction is LubAuction)
            {
                type = "Lowest unique wins";
            }
            else if (auction is EsfAuction)
            {
                type = "Highest wins";
            }

            return type;
        }
    }

With the above view code and model, get the following error on the line marked in bold, why is this?

The best overloaded method match for 'UI.Models.AuctionListViewModel.GetAuctionType(UI.AuctionService.Auction)' has some invalid arguments

Upvotes: 0

Views: 664

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038790

Due to the dynamic (weak) typing of the WebGrid helper you need a cast:

grid.Column(
    columnName: "", 
    header: "Type", 
    format: (auction) => AuctionListViewModel.GetAuctionType((Auction)auction.Value)
)

I would recommend you using better grid helpers such as MvcContrib Grid and Telerik Grid which will give you strong typing and compile time safety.

Upvotes: 1

nemesv
nemesv

Reputation: 139758

In the grid.Column method's format parameter's parameter (in your case auction) you get the actual item (an Auction) but it's wrapped into a dynamic wrapper called WebGridRow.

You can use your properties on this wrapper and it delegates to the actual item e.g: auction.Title will work, but if you want to get the whole item (the Auction) you need to use the Value property of the WebGridRow.

format: auction => 
      uctionListViewModel.GetAuctionType(((WebGridRow)auction).Value)

Upvotes: 2

Related Questions