得到嘗試加載鑑於此錯誤消息:將正確的類型從Controller傳遞給View?
The model item passed into the dictionary is of type
'System.Data.Entity.Infrastructure.DbQuery`1[<>f__AnonymousType0`2[System.Int32,System.String]]',
but this dictionary requires a model item of type
'System.Collections.Generic.IEnumerable`1[HelloWorld.Models.P]'.
難道是從沒有從控制器傳遞正確的類型有何看法?
這裏的模型:
public class P
{
[Key]
public virtual int ImportID { set; get; }
public virtual string MicroName { set; get; }
}
這裏的的DbContext定義:
public DbSet<P> Ps { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//REMAPPING TABLE NAMES
modelBuilder.Entity<P>()
.ToTable("SomeTable_With_Really_LongName_tbl");
base.OnModelCreating(modelBuilder);
}
這裏的控制器動作:
public ActionResult ListP()
{
var model = (from p in _db.Ps
select new
{
p.ImportID,
p.MicroName
}).Take(10);
return View(model);
}
這裏的觀點:
@model IEnumerable<HelloWorld.Models.P>
@{
ViewBag.Title = "List P";
}
<h2>List P</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
ImportID
</th>
<th>
MicroName
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ImportID)
</td>
<td>
@Html.DisplayFor(modelItem => item.MicroName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
@Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</table>
有什麼建議嗎?
確定;當我回去工作時我會嘗試。謝謝! – scv
邁克爾, 對不起,我有一個'訂購'右選擇新之前。 你如何去設置訂單? 再次感謝您的幫助。 – scv