我有一个巨大的表,其中包含一些列空数据,但我不想更改表中的任何内容。在我的一个视图中,我只想选择两列显示在页面中,第一列是ID,第二列是Area,但在Area中,我应该使用distinct并防止显示空值有我的代码,但是它不起作用,请告诉我我该怎么做。
public class ClsArea
{
public int id { get; set; }
public string AreaName { get; set; }
}
public ActionResult Index()
{
return View(DB.school.ToList());
}
----------
@model IEnumerable<school.Models.ClsArea>
<div class="container">
<div class="row" style="margin-top:30px">
<div class="col-lg-12 col-xs-12">
<select class="form-control">
@foreach (var itemArea in Model.Select(c => new { c.IdSMP, c.Area }).ToList() as IEnumerable<ClsArea>)
{
<option [email protected]>
@itemArea.areaname
</option>
}
</select>
</div>
</div>
</div>
错误显示
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[<>f__AnonymousType1`2[System.Int32,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IList`1[PMIS_V01.Models.ClsArea]'.
Distinct
不会阻止NULL
值。它并非旨在这样做。它将与其他值一起拉一个NULL
值。
如果要跳过NULL
值,那么为什么不忽略它们呢?类似于:
DB.school.where(s => s.Area != null).ToList()
更新:
您希望在视图中显示school.Models.ClsArea
的列表,但正在从操作发送DB.school
类型的列表。 DB.school
实体模型类型正确吗?