我对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