将模型数据传递给MVC 5中的视图

时间:2014-09-18 18:21:27

标签: c# linq asp.net-mvc-5

我对MVC 5 .NET的世界不熟悉......我正试图从Ignite UI中了解Tree Control。

我需要从模型中检索我的数据并将其传递给我的视图......但是,我不断收到以下错误:

  

传递到字典中的模型项的类型是' 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()部分不正确,我真的更关心的是将数据放到我的视图中(此时)。我只想包括我所拥有的所有代码......

3 个答案:

答案 0 :(得分:2)

问题是视图期望的数据不同于您为控制器提供的数据。

在你的观点中:

@model IQueryable<DataViewer.Models.Viewer>

表示该视图需要IQueryable&lt;&gt; Viewer的集合,但在你的控制器内:

// GET: /Viewer/
public ActionResult Index()
{
    var result = db.Items.Select(i => i.GeoName).Distinct().OrderBy(n => n);

    return View(result);
}

您正在传递字符串集合。

您需要返回Viewer,或更改视图的模型。

答案 1 :(得分:2)

您将错误的类型传递给您的观点。查看视图所期望的模型类型:

@model IQueryable<DataViewer.Models.Viewer>

Viewer个对象的可查询集合。但看看你给它的是什么:

db.Items.Select(i => i.GeoName).Distinct().OrderBy(n => n);

GeoName个属性的集合。 GeoName string是否有{{1}}? (错误意味着它是。)

如果您的模型需要一组对象,则需要为其提供此类集合。你不能只给它一些字符串并期望它将这些字符串转换成对象。

答案 2 :(得分:1)

将您的行动结果更改为此...

public ActionResult Index()
    {
                    //  var result = db.Items.Select(i => i.GeoName).Distinct().OrderBy(n => n);
         Viewer model = new Viewer();
         model = (from x in db.Items
                 select new Viewer
                 {
                   GeoPSRType = x.FieldName
                  }).Distinct().FirstOrDefault().OrderBy(x => x.SomeField);


        return View(model);
    }

该查询中可能存在一些语法错误,但这使您可以大致了解需要传递到视图中的内容