首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

MVC中怎么构建类型IEnumerable<T>的模型项

2012-04-28 
MVC中如何构建类型IEnumerableT的模型项接触MVC不久,在项目中遇到了下面的问题:有一张表Customers,我现

MVC中如何构建类型IEnumerable<T>的模型项
接触MVC不久,在项目中遇到了下面的问题:

  有一张表Customers,我现在只想取出其中的一个字段ContactName在List页面展示出来,代码如下。

C# code
public ActionResult Index(){    var q = from c in ltdb.Customers select c.ContactName;    return View(q);}


  但是代码执行过程中会报错,错误如下。
传入字典的模型项的类型为“System.Data.Linq.DataQuery'1[System.String]”,但此字典需要类型“System.Collections.Generic.IEnumerable'1[MvcApplication3.Models.Customers]”的模型项。

  现在是不是需要将“from c in ltdb.Customers select c.ContactName”的结果转换成为“IEnumerable<T>”类型返回呢,

  如果是的话,怎么将其转换成为正确的模型项?

麻烦高手帮忙指点一下,不胜感激。

[解决办法]
问题是你当前页面接收的类型是怎样的?
[解决办法]
我没用过Linq,但是我觉得报的这个错误的原因是你定义的是IEnumerable<MvcApplication3.Models.Customers>和你赋值的是IEnumerable<string>是不对的。你把IEnumerable<MvcApplication3.Models.Customers>改成IEnumerable<string>试试。
[解决办法]
类型选择错误吧?c.ContactName是什么类型的?返回的是什么类型?

热点排行