Hi; every kind of solution binding data webgrid static models. But my Data is dynamic. Please dont say: why do you use DataTable?
Controller:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(string submitButton, FormCollection fc)
{
ViewData["Customers"] = new SelectList(CustomerOperation.GetCustomers().Items, "Id", "Name", null);
ViewData["Jobs"] = new SelectList(JobOperation.GetCustomersAssemblyList().Items, "scheduleId", "name", null);
int Id = ConvertUtil.ToInt(fc["Customers"]);
int scheduleId = ConvertUtil.ToInt(fc["Jobs"]);
DataTable dt = JobOperation.GetJobsBySchedulerIdAndCustomerId(scheduleId, Id);
return View(Json(dt,JsonRequestBehavior.AllowGet));
}
View:
<%
List<WebGridColumn> cols = new List<WebGridColumn>();
WebGrid grid = null;
if (Model != null)
{
grid = new WebGrid(source: Model.Data, rowsPerPage: 3);
System.Reflection.PropertyInfo[] propertiesofColumn = new System.Reflection.PropertyInfo[] { };
foreach (object column in Model)
{
propertiesofColumn = column.GetType().GetProperties();
}
foreach (System.Reflection.PropertyInfo propInfo in propertiesofColumn)
{
cols.Add(grid.Column(propInfo.Name, propInfo.Name));
}
}
using (Html.BeginForm())
{ %>
Error is occured on grid = new WebGrid(source: Model.Data, rowsPerPage: 3);.
ERROR:
RunTimeBinderException: The best overloaded method match for 'System.Web.Helpers.WebGrid.WebGrid(System.Collections.Generic.IEnumerable, System.Collections.Generic.IEnumerable, string, int, bool, bool, string, string, string, string, string, string, string)' has some invalid arguments
See Question&Answers more detail:os