我對MVC 5 .NET的世界感到陌生......我試圖從Ignite UI開始探索樹控制。傳遞模型數據以在MVC中查看5
我需要從我的模型找回我的數據,並把它傳遞給我的看法......不過,我不斷收到以下錯誤:
The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery
1[System.String]', but this dictionary requires a model item of type 'System.Linq.IQueryable
1[DataViewer.Models.Viewer]'.
這裏是我的模型:
namespace DataViewer.Models
{
[Table("dbo.table")]
public class Viewer
{
[Key, Column(Order=0)] public long id { get; set; }
public int Revision { get; set; }
public string GeoPSRType { get; set; }
public string GeoName { get; set; }
public string L2Name { get; set; }
public string L3Name { get; set; }
}
}
等
我的控制器:
namespace DataViewer.Controllers
{
public class ViewerController : Controller
{
private ViewerDBContext db = new ViewerDBContext();
public ActionResult Index()
{
var result = db.Items.Select(i => i.GeoName).Distinct().OrderBy(n => n);
return View(result);
}
}
}
而我的觀點:
@model IQueryable<DataViewer.Models.Viewer>
@using Infragistics.Web.Mvc
@(Html.Infragistics().Tree()
.DataSource(Model)
.Bindings(b =>
b.TextKey("GeoName")
.PrimaryKey("ID")
.ValueKey("ID")
.ChildDataProperty("L2PSRType")
)
)
.DataSource(Model)
.LoadOnDemand(true)
.DataSourceUrl(Url.Action("tree-data-on-demand"))
.DataBind()
.Render()
)
我想通的問題是,我應該用
IQueryable<Viewer> result =
db.Items.Select(i => i.GeoName).Distinct().OrderBy(n => n);
路過一個強類型的返回類型,但拋出不同的錯誤:
InvalidOperationException: The model item passed into the dictionary is of type
'System.Data.Entity.Infrastructure.DbQuery`1[System.String]',
but this dictionary requires a model item of type
'System.Linq.IQueryable<DataViewer.Models.Viewer>.'
An explicit conversion exists, are you missing a cast?
這是關於我在強類型語法上很生鏽的事實,還是我只是一個總的栗色(或是它的組合)?
P.S.另外,我毫不懷疑視圖的Infragistics()。Tree()部分是不正確的,我更關心如何讓我的數據進入我的視圖(在這一點上)。我只是想包括我所有的代碼...
你告訴什麼一個大致的瞭解該視圖接受類型爲'IQueryable'的模型,但是您沒有將該類型傳遞給視圖。而是傳遞數據庫查詢的結果。您需要'返回查看(* modelClass *)' –
CSharper
2014-09-18 18:25:04