-1

你好; 各种解决方案绑定数据webgrid静态模型。但我的数据是动态的。请不要说:你为什么使用DataTable?

控制器:



      [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));
        }

看法:


 <% 
     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())
      { %>

grid = new WebGrid(source: Model.Data , rowsPerPage: 3); .

错误:

RunTimeBinderException:最佳重载方法匹配 'System.Web.Helpers.WebGrid.WebGrid(System.Collections.Generic.IEnumerable, System.Collections.Generic.IEnumerable, string, int, bool, bool, string, string, string, string , string, string, string)' 有一些无效参数

4

1 回答 1

2

请不要说:你为什么使用DataTable?

使用 DataTable 没有任何问题。DataTables 早在 .NET 时代就已经存在,完全可以理解的是,仍有大量现有代码依赖于它们。但这不是为什么那些 DataTables 应该跨越从控制器到视图的边界的原因。控制器应始终将视图模型传递给视图。所以让我们从定义这个视图模型开始,我们应该:

public class MyViewModel
{
    public int? SelectedCustomerId { get; set; }
    public IEnumerable<SelectListItem> Customers { get; set; }

    public int? SelectedJobId { get; set; }
    public IEnumerable<SelectListItem> Jobs { get; set; }

    public IEnumerable<string> Columns { get; set; }
    public IEnumerable<object> Values { get; set; }
}

我们还将为请求定义一个视图模型,以避免您当前正在执行的丑陋解析:

public class RequestViewModel
{
    public string SubmitButton { get; set; }
    public int Customers { get; set; }
    public int Jobs { get; set; }
}

现在,我们如何隐藏它DataTable?我们可以编写一个扩展方法,将其转换为动态对象:

public static class DataTableExtensions
{
    private sealed class Row : DynamicObject
    {
        private readonly DataRow _row;
        public Row(DataRow row) 
        { 
            _row = row; 
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            var value = _row.Table.Columns.Contains(binder.Name);
            result = value ? _row[binder.Name] : null;
            return value;
        }
    }

    public static IEnumerable<dynamic> AsDynamicEnumerable(this DataTable table)
    {
        return table.AsEnumerable().Select(row => new Row(row));
    }
}

到目前为止,一切都很好。难题的最后一个平静是在控制器动作中,我们将在其中创建我们的视图模型:

[HttpPost]
public ActionResult Index(RequestViewModel request)
{
    int id = request.Customers;
    int scheduleId = request.Jobs;
    DataTable dt = JobOperation.GetJobsBySchedulerIdAndCustomerId(scheduleId, id);

    // Now let's build the view model for the result:
    var model = new MyViewModel();
    model.Columns = dt.Columns.Cast<DataColumn>().Select(x => x.ColumnName);
    model.Values = dt.AsDynamicEnumerable();
    model.Customers = new SelectList(CustomerOperation.GetCustomers().Items, "Id", "Name");
    model.Jobs = new SelectList(JobOperation.GetCustomersAssemblyList().Items, "scheduleId", "name");

    return View(model);
}

现在我们当然可以有一个强类型视图:

<%
    var grid = new WebGrid(Model.Values);
    var columns = Model.Columns.Select(x => grid.Column(x));
%>

<%= grid.GetHtml(columns: columns) %>

// and then we could have dropdowns and other stuff
<%= Html.DropDownListFor(x => x.SelectedCustomerId, Model.Customers, "-- customer --") %>
<%= Html.DropDownListFor(x => x.SelectedJobId, Model.Jobs, "-- job --") %>
于 2012-07-03T09:23:05.760 回答