简体   繁体   中英

Passing model data to view in MVC 5

I am darned new to the world of MVC 5 .NET... I am trying to get my head around the Tree Control from Ignite UI.

I need to retrieve my data from my model and pass it to my view... However, I keep getting the following error:

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]'.

Here is my model:

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; }
    }
}

etc.

And my Controller:

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);
        }
    }
}

And my view:

@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()
)

I figured the issue was that I should be passing a strongly typed return-type by using

IQueryable<Viewer> result = 
db.Items.Select(i => i.GeoName).Distinct().OrderBy(n => n);

but that throws a different error:

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?

Is this relating to the fact that I'm pretty rusty on strongly typed syntax, or am I just being a total maroon (or is it a combination)?

PS Also, I have no doubt that the Infragistics().Tree() portion of the view is not correct, I am really more concerned about getting my data to my view (at this point). I just wanted to include all code I had...

The problem is the view is expecting different data than you're providing it with your controller.

Inside your view:

@model IQueryable<DataViewer.Models.Viewer>

means the view is expecting an IQueryable<> collection of Viewer's, but inside your controller:

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

    return View(result);
}

You are passing a collection of strings.

You need to either return Viewer's, or change the model of the view.

You're passing the wrong type to your view. Look at the type of model the view expects:

@model IQueryable<DataViewer.Models.Viewer>

A queryable collection of Viewer objects. But look at what you're giving it:

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

A collection of GeoName properties. Is GeoName by any chance a string ? (The error implies that it is.)

If your model is expecting a collection of objects, you need to give it such a collection. You can't just give it some strings and expect it to convert those strings into objects.

Change your Action Result to this...

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);
    }

There's probably some syntax errors in that query but this gives you a general understanding of what you need to pass into the View

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM