我嘗試將一個列表傳入我的視圖,但我得到以下錯誤:CS1963:表達式樹可能不包含動態操作在我的視圖,在行@ html.displayfor ... 。razor @foreach問題與列表
public class ColNameLink
{
public string ColName { get; set; }
public List<string> AvaiableDBcolumns { get; set; }
}
public class ViewModelListColumns
{
public List<ColNameLink> ColList { get; set; }
}
控制器
var columnNames = (from dc in dt.Columns.Cast<DataColumn>()
select dc.ColumnName).ToList();
var AvailLinks = new List<string>();
AvailLinks.Add("FirstName");
AvailLinks.Add("SurName");
var viewModel = new ViewModelListColumns() { ColList = new List<ColNameLink>() };
columnNames.ForEach(colName => viewModel.ColList.Add(new ColNameLink { ColName = colName, AvaiableDBcolumns = AvailLinks }));
return View("ChooseColumns", viewModel);
查看
@Model uploabulk.ViewModels.ViewModelListColumns
<table class="table">
<tr>
<th>Column name</th>
<th>Database column</th>
<th>Include?</th>
</tr>
@foreach (var colList in Model)
{
foreach (var column in colList)
{
<tr>
<td>@Html.LabelFor(m => column.ColName) </td>
<td>Html.DropDownListFor("Selection", new SelectList(column.AvaiableDBcolumns, "AvailableDBcolumns"))</td>-->
<td></td>
</tr>
}
}
</table>
你想DropDownListFor確定而不是DropDownList? Razor中所有'... For'方法的第一個參數是一個表達式(例如'm => m.AvailableDBcolumns')。 – 2015-03-02 21:28:43