[需要建议]请帮助...动态列表绑定 - 非常[NOT]紧急.. [英] [Need Suggestion] Kindly Help... Dynamic list binding - Very[NOT] Urgent..
问题描述
请看下面的代码
Please take a look at the below code
public ActionResult SampleDownload1()
{
var records = _getSampleInfoQuery.Execute();
var columns = new[]
{
Column(x => x.College),
Column(x => x.Amount, ExcelFormat.Money),
Column(x => x.CreatedDate, ExcelFormat.Date, "Date"),
Column(x => x.CreatedBy),
Column(x => x.CreatedDate, ExcelFormat.Time, "Time"),
Column(x => x.PercentageExample, ExcelFormat.Percent),
};
return new ExcelFileResult<SampleInfo>(records) {ColumnDefinitions = columns};
}
private static ExcelColumnDefinition Column(Expression<Func<SampleInfo, object>> member, string format = null, string header = null)
{
return ExcelColumnDefinition.Create(member, format, header);
}
我的要求是我要为 Var列
动态,这意味着我希望用户指定要添加到
My requirement is i want to assign values to Var columns
dynamically, meaning i would like the user to specify the list of columns to be added to
var columns;
我可能会传递所选列的ID数组,如 College,Amount, PercentageExample
...我的系统必须只添加那些列。
请注意我是否以正确的方式提出问题。让我知道任何澄清。
感谢您宝贵的时间:)
Vishnu
I would probably be passing array of IDs of the chosen column like College,Amount,PercentageExample
... My system has to add only those columns.
Note sure whether i am putting my question in a right way. Let me know for any clarifications.
Thanks for your precious time :)
Vishnu
推荐答案
好吧,目前你通过直接将它们分配为数组参数来获取列。如果您想要选择单个项目,那么您需要做一些不同的事情 - 这是您可以根据需要调整的示例:
Well, at the moment you are picking up your columns by directly allocating them as array arguments. If you are wanting to pick individual items, you're going to need to do something a little bit different - here's an example that you could adapt as you see fit:
public ActionResult SampleDownload1(List<string> columnIds)
{
var records = _getSampleInfoQuery.Execute();
List<excelcolumndefinition> columns = new List<excelcolumndefinitions>();
foreach (string columnName in columnIds)
{
switch (columnName)
{
case "College":
columns.Add(Column(x => x.College));
break;
case "Amount":
columns.Add(Column(x => x.Amount, ExcelFormat.Money));
break;
// And so on....
}
}
return new ExcelFileResult<sampleinfo>(records) {ColumnDefinitions = columns.ToArray()};
}
显然这是一种相当幼稚,过于简单的方法,但它应该可以为你提供一些想法。
Obviously this is a fairly naive, simplistic approach, but it should serve to give you some ideas.
这篇关于[需要建议]请帮助...动态列表绑定 - 非常[NOT]紧急..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!