Solved

but this dictionary requires a model item of type

Posted on 2015-01-02
6
165 Views
Last Modified: 2015-01-04
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MobileWeb.Models.ProductDetailsModel]', but this dictionary requires a model item of type 'MobileWeb.Models.ProductDetailsModel'.

Keep receiving this error, I see the data when it reaches the return view but I get this error.

 public class ProductDetails
    {

        public List<ProductDetailsModel> data { get; set; }
    }
    public class ProductDetailsModel
    {
               
        public string datanumber { get; set; }
        public string name { get; set; }
        public string description { get; set; }
        public string number { get; set; }
        public float avg_price { get; set; }

    }

Open in new window


  Stream objStream;
                objStream = wrGETURL.GetResponse().GetResponseStream();
              
                StreamReader objReader = new StreamReader(objStream);

                string sLine = "";
                int i = 0;
                WebResponse response = wrGETURL.GetResponse();
                string contentType = response.ContentType;
                //while (sLine != null)
                //{
                //    i++;
                sLine = objReader.ReadLine();
                 JObject o = JObject.Parse(sLine);
             //    Console.WriteLine("description: " + o["description"]);
                 List<ProductDetailsModel> mydata = new List<ProductDetailsModel>()
                 {
                   new ProductDetailsModel { description = (o["description"]).ToString() }
                 };

                 return View(mydata.AsEnumerable());

Open in new window

0
Comment
Question by:sevensnake77
  • 3
  • 2
6 Comments
 
LVL 7

Assisted Solution

by:Camillia
Camillia earned 167 total points
Comment Utility
I think you need to change your View to have this on top

@model IEnumerable<WebApplication1.Models.ProductDetailsModel>

Open in new window


It's probably like this now

@model WebApplication1.Models.ProductDetailsModel

Open in new window

0
 
LVL 35

Assisted Solution

by:Miguel Oz
Miguel Oz earned 333 total points
Comment Utility
Actually there is no need to convert to an enumerable if your code is changed as follows:
Controller: Returns the list, in fact you can use any object as parameter.
return View(mydata);

Open in new window

View: Define model using mydata type (List<ProductDetailsModel>) and access each item by iterating directly from model as shown below:
@model List<'MobileWeb.Models.ProductDetailsModel>
@* and property Model is type of List<ProductDetailsModel> *@
@foreach(var item in Model)
{
    <span>@item.name</span>
}

Open in new window

where "MobileWeb.Models" is current namespace where ProductDetailsModel is defined.  (Change it if this is not correct)

Note: Another alternative could be:
Controller Create a ProductDetails instance and return View(instance).
View: Define model as ProductDetails and when you need to access an item iterate  Model.data
0
 
LVL 9

Author Comment

by:sevensnake77
Comment Utility
List<ProductDetailsModel> mydata = new List<ProductDetailsModel>()
Line 112:                 {

Open in new window

Object reference not set to an instance of an object.

Recieving this error now
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 35

Accepted Solution

by:
Miguel Oz earned 333 total points
Comment Utility
Please be more specific, the posted line seems correct unless the following line set o as null. (JObject o = JObject.Parse(sLine);), thus this construct will fail: o["description"]).
IF that is the case you need to add conditional logic to avoid adding a new item to list.
0
 
LVL 9

Author Comment

by:sevensnake77
Comment Utility
ok I need a little help with that, I can do that from a database but I have the data with sLine

how can I return the data description with this list
 List<LookupTable> mydata = new List<LookupTable>()
                 {
                   new LookupTable { description = (o["description"]).ToString() }
                 };

Open in new window

0
 
LVL 9

Author Closing Comment

by:sevensnake77
Comment Utility
Ok it was simple I just had to

var model = new context
                 {
                   description =(o["description"]).ToString()
                 };
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now