我正在尝试制作一个可编辑的表格。我有一个动态表,可以使列可见或不可见。我有一个带有编辑按钮的额外列。当我点击它时,我会进行 Ajax 调用以获取该行的可编辑版本。
我的视图模型:
public class List: IDynamicGridDataModel<View>
{
public List()
{
ColumnOptions = new ColumnOptions();
ColumnOptions.SelectedColumns = new List<string>() { "Address", "ZipCode", "City", "Country" };
ColumnOptions.DisabledColumns = new List<string>() { "Id" };
Items = new List<View>();
}
#region IDynamicGridDataModel<View> Members
public ColumnOptions ColumnOptions { get; set; }
public GridModel<View> GridModel { get; set; }
public IEnumerable<View> Items { get; set; }
#endregion
}
物品在哪里:
public class View
{
public long Id { get; set; }
public string Address { get; set; }
public string ZipCode { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
我有以下扩展来创建表格行:
public static MvcHtmlString EditorRowFor<TModel, TRow>(this HtmlHelper<TModel> html, Expression<Func<TModel,TRow>> expression, IEnumerable<string> selectedColumns)
{
var metadataProvider = new DataAnnotationsModelMetadataProvider();
var stringBuilder = new StringBuilder();
stringBuilder.Append("<tr>");
foreach (var column in selectedColumns)
{
var entityParam = Expression.Parameter(expression.ReturnType);
var columnLambda = Expression.Lambda(Expression.Property(entityParam, typeof(TRow), column));
var selectCall = Expression.Call(typeof(Queryable),
"Select",
new Type[] { typeof(TModel), columnLambda.Body.Type },
expression,
columnLambda);
html.EditorCellFor(selectCall);
stringBuilder.AppendLine("</td>");
}
stringBuilder.AppendLine("</tr>");
return new MvcHtmlString(stringBuilder.ToString());
}
从我的角度来看,这个函数是这样调用的:
@Html.EditorRowFor(m => m.Items.Single(), Model.ColumnOptions.SelectedColumns);
EditorCellFor 方法签名:
public static MvcHtmlString EditorCellFor<TModel, TItem>(this HtmlHelper<TModel> html, Expression<Func<TModel, TItem>> expression)
我目前的问题是创建 selectCall。我想为 Lambda 表达式创建一个表达式:
m => m.Items.Single().Select(i => i.Address)
我收到以下错误:
No generic method 'Select' on type 'System.Linq.Queryable' is compatible with
the supplied type arguments and arguments. No type arguments should be
provided if the method is non-generic.
我整个早上都在寻找这个问题,但到目前为止还没有运气。