2015-05-07 127 views
1

我目前正在学习C#.net,我试图显示一个不同的状态列表,但我无法弄清楚如何做到这一点。显示字符串列表

在我的控制,我有:

public ActionResult StateListDistinct() 
     { 
      var distinctStates = (from w in db.Contact_Addresses 
            select new { State = w.Site_address_state}).Distinct(); 

      return View(distinctStates.ToList()); 
     } 

在我看来,我有:

@model List<String> 
<table class="table"> 

@foreach (var item in Model) { 
    <tr> 

     <td> 
      @Html.DisplayFor(model => item) 
     </td> 

    </tr> 
} 

</table> 

我越来越

模型项目传递到字典中的错误是类型'System.Collections.Generic.List`1 [<> f__AnonymousType1`1 [System.String]]',但是这个字典需要一个ty的模型项pe'System.Collections.Generic.List`1 [System.String]'。

我需要做些什么来显示状态列表?

+0

'选择新w.Site_address_state'假设Site_address_state是字符串类型。 –

回答

4

您的观点期望得到一个字符串列表,但是您提供的是一个匿名类型列表。

使用方法的语法,你想达到什么可以做这种方式:

db.Contact_Addresses.Select(state =>state.Site_address_state).Distinct().ToList(); 

这应该做的伎俩还有:

var distinctStates = (from w in db.Contact_Addresses 
           select w.Site_address_state).Distinct().ToList();