我試圖從控制器發送一個列表到視圖。我認爲它會綁定,但它是一種不同的類型,所以它不會。 (錯誤:參數類型'System.Collections.Generic.List'不能分配給模型類型'System.Collections.Generic.IEnumerable')。那麼我該如何映射這兩個列表呢?如何在asp.net MVC中映射2個不同列表
控制器
public ActionResult MyData()
{
var oldList = db.oldList.Select(x=>x.Name).ToList();
// probably here i should add var newList and in some way map with oldList then return to view
return View(oldList);
}
新的列表視圖模型
public class NewListViewModel
{
public string Name { get; set; }
public int Count { get; set; }
}
我的視圖(邁德特)
@model IEnumerable<MyApp.ViewModels.NewListViewModel>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Count)
</th>
</tr>
@foreach (var item in Model)
{
using (Html.BeginForm())
{
<tr>
<td>
@Html.TextBoxFor(modelItem => item.Name)
</td>
<td>
@Html.TextBoxFor(modelItem => item.Count)
</td>
</tr>
}
}
</table>
您選擇'Name',那麼你的''oldList是'IEnumerable的'但你的視圖等待'IEnumerable ' –
我知道它,但我想從舊列表中獲取每個名稱並將它們添加到newList。 – DiPix