Solved

but this dictionary requires a model item of type

Posted on 2015-01-02
6
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 7

Assisted Solution

by:Camillia
Camillia earned 167 total points
ID: 40528752
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 36

Assisted Solution

by:Miguel Oz
Miguel Oz earned 333 total points
ID: 40529898
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
ID: 40530651
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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 36

Accepted Solution

by:
Miguel Oz earned 333 total points
ID: 40530696
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
ID: 40530821
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
ID: 40530921
Ok it was simple I just had to

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

740 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