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
There's nothing wrong in using DataTable. DataTables have been around since the early ages of .NET and it is completely understandable that there is still lots of existing code that relies on them. But that's not a reason why those DataTables should traverse the frontiers from the controller to the view. A controller should always pass a view model to the view. So let's start by defining this view model, shall we:
We shall also define a view model for the request in order to avoid the ugly parsing that you are currently performing:
Now, how do we hide this
DataTable
? We could write an extension method which will convert it to a dynamic object:So far so good. The last peace of the puzzle is in the controller action where we shall create our view model:
and now we could of course have a strongly typed view: