Solved

but this dictionary requires a model item of type

Posted on 2015-01-02
6
181 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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