I've been stuck on this error for almost a day now and I don't understand why I am getting it.
The error:
An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll but was not handled in user code
Additional information: The best overloaded method match for 'System.Web.Helpers.WebGrid.Bind
Controller:
var data = (from brand in db.Brand
from division in db.Division
from supplier in db.Supplier
where brand.BrandID == division.DivisionID
where product.SupplierID == supplier.SupplierID
select new ModelGrid
{
BrandID = brand.BrandID,
BrandName = brand.BrandName,
DivisionName = division.DivisionName,
CompanyName = supplier.CompanyName,
ContactName = supplier.ContactName,
Country = supplier.Country
}).ToList();
return View(data);
View:
WebGrid grid = new WebGrid(Model);
grid.Bind(Model, rowCount: ViewBag.count, autoSortAndPage: false);
@grid.GetHtml(
columns: grid.Columns(
grid.Column("BrandID", "Brand ID", canSort: true),
grid.Column("BrandName", "Brand Name", canSort: true),
grid.Column("DivisionName", "Division Name", canSort: true),
grid.Column("CompanyName", "Company Name", canSort: true),
grid.Column("ContactName", "Contact Name", canSort: true),
grid.Column("Country", "Country", canSort: true)
)
)
and Model:
public class ModelGrid
{
public int BrandID { get; set; }
public string BrandName { get; set; }
public string DivisionName { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
public string Country { get; set; }
}
From what I can find it seems you get this error when using anonymous types, however I thought I had avoided this. I am still learning at this point so any advice is much appreciated.
Try this and see if it works
grid.Bind(Model, rowCount: (int)ViewBag.count, autoSortAndPage: false);
I have a feeling its failing because ViewBag is a dynamic type and it doesn't know that Count is an int and Bind method expects an int for rowCount.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.