我正在创建一个获取位置类型的应用程序
我收到此错误
Model item passed into the dictionary is of type 'System.Collections.Generic.List[CA1API.Models.Weather]', but this requires model item type 'System.Collections.Generic.IEnumerable [CA1API.Models.Location]'.
我创建了一个看起来像这样的ViewModel 命名空间CA1API.Models
{
public enum County
{
Ireland,
England,
Iceland
}
public class Location
{
public int LocationID { get; set; }
[Display(Name = "Location")]
public County LocationName { get; set; }
public double Lat { get; set; }
public double Lon { get; set; }
public int WeatherID { get; set; }
public List<Weather> Weathers { get; set; }
}
}
我的控制器看起来像这样
public class HomeController : Controller
{
private WeatherDb db = new WeatherDb();
public ActionResult Index()
{
return View(db.Weathers.ToList());
}
}
我的观点看起来像这样
@model IEnumerable<CA1API.Models.Location>
<div class="row">
<div class="col-md-2">
<div class="panel panel-success">
<div class="panel-heading">County</div>
<div>
<div class="panel-body" style="overflow-x:hidden; height:300px;">
@foreach (var item in Model)
{
<p>modelItem=>item.County</p>
}
</div>
</div>
</div>
</div>
答案 0 :(得分:0)
您的Index
查看使用@model IEnumerable<CA1API.Models.Location>
作为模型,并尝试将@model List<CA1API.Models.Weather>
传递给图强>
因为您尝试传递的模型不是继承,或者它不是预期会被接收的模型,所以它会抛出异常。
传递到字典中的模型项是类型 &#39; System.Collections.Generic.List [CA1API.Models.Weather]&#39;,但是这个 需要模型项类型&#39; System.Collections.Generic.IEnumerable [CA1API.Models.Location]&#39;
答案 1 :(得分:0)
错误很清楚。您传递的视图需要List<Weather>
IEnumerable<Location>
。{/ p>
您可能需要return View(db.Locations.ToList());
而不是db.Weathers
。