Using MVC3 WebGrid - Column does not exist error
asp.net-mvc-3, webgrid
Solution
Your UserVM class is using fields instead of properties. Will not work with webgrid. Here's your class with properties:
public class UserVM
{
public int UserID { get; set; }
public string Name { get; set; }
}
Problem
I am getting this error - System.InvalidOperationException: Column "UserID" does not exist. This is my View Model. ``` public class UserVM { public int UserID; public string Name; } ``` This is my action method in the controller. ``` public ActionResult TestGrid() { List<UserVM> rows = new List<UserVM>(); rows.Add(new UserVM { UserID = 100, Name = "Abc" }); rows.Add(new UserVM { UserID = 101, Name = "Def" }); rows.Add(new UserVM { UserID = 102, Name = "Ghi" }); return View("TestGrid", rows.AsEnumerable()); } ``` This is my view. ``` @model IEnumerable<Module.ViewModels.UserVM> @{ var grid = new WebGrid(Model); } <div> @grid.GetHtml( columns: grid.Columns( grid.Column("UserID", header: "User ID"), grid.Column("Name", header: "Name") ) ) </div> ``` That's it. Any idea why I am getting the error?