如何映射在asp.net MVC 2不同势名单 [英] How to map 2 diffrent List in asp.net MVC
本文介绍了如何映射在asp.net MVC 2不同势名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从控制器发送一个列表中查看。我认为这将绑定,但它是一个不同的类型,因此它不会。的(错误:参数类型'System.Collections.Generic.List'是不能分配给模拟型System.Collections.Generic.IEnumerable')的。所以,我怎么映射两个列表?
控制器
公众的ActionResult迈德特()
{
VAR oldList = db.oldList.Select(X => x.Name).ToList();
//这里大概我应该补充VAR newList并与oldList某种方式的地图,则返回到查看
返回查看(oldList);
}
新列表视图模型
公共类NewListViewModel
{
公共字符串名称{;组; }
公众诠释计数{搞定;组; }
}
我的视图(迈德特)
@model IEnumerable的< MyApp.ViewModels.NewListViewModel>
<表类=表>
< TR>
<第i个
@ Html.DisplayNameFor(型号=> model.Name)
< /第i
<第i个
@ Html.DisplayNameFor(型号=> model.Count)
< /第i
< / TR>
@foreach(以型号VAR项)
{使用
(Html.BeginForm())
{
< TR>
< TD>
@ Html.TextBoxFor(modelItem => item.Name)
< / TD>
< TD>
@ Html.TextBoxFor(modelItem => item.Count)
< / TD>
< / TR>
}
}
< /表>
解决方案
您几乎拥有了:
公众的ActionResult迈德特()
{
名单,LT; NewListViewModel> newList =
db.oldList.Select(X =>新建NewListViewModel {名称= x.Name})了ToList();
返回查看(newList);
}
I was trying to send a list from Controller to View. I thought it would bind, but it's a different type so it won't. (error: Argument type 'System.Collections.Generic.List' is not assignable to model type 'System.Collections.Generic.IEnumerable'). So how am I supposed to map both list?
Controller
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);
}
New list ViewModel
public class NewListViewModel
{
public string Name { get; set; }
public int Count { get; set; }
}
My View (MyData)
@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>
解决方案
You almost had it:
public ActionResult MyData()
{
List<NewListViewModel> newList =
db.oldList.Select(x=>new NewListViewModel { Name = x.Name}).ToList();
return View(newList);
}
这篇关于如何映射在asp.net MVC 2不同势名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文