Solved

but this dictionary requires a model item of type

Posted on 2015-01-02
6
167 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
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 35

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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 …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

947 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

23 Experts available now in Live!

Get 1:1 Help Now