Solved

but this dictionary requires a model item of type

Posted on 2015-01-02
6
172 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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