0

Why can't I retrieve data from table? This shows an error

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery1[<>f__AnonymousType11[System.String]]', but this dictionary requires a model item of type 'A.Models.AboutU'.

Controller

{
    var selectResult = db.AboutUs.Select(s => new { des = s.Description });
    return View(selectResult);
}

View

@model A.Models.AboutU

<p>
    @Html.HiddenFor(model => model.Description)
    @Html.DisplayFor(model => model.Description)
</p>

I have the value in the table and beeds to retrieve the description column in the table. But it seems to occur an error and not able to get the value, while using break point it shows the value in selectResult is null. Why is that getting value? Do I need to change my controller part?

athira
  • 19
  • 6
  • 1
    Possible duplicate of [The model item passed into the dictionary is of type .. but this dictionary requires a model item of type](http://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) –  Feb 08 '17 at 06:17
  • Because your query generates `IQueryable` not a single instance of `AboutU` –  Feb 08 '17 at 06:18
  • but how can i fix the error ? should i need to give IQueryable to controller – athira Feb 08 '17 at 06:22
  • IQueryable =db.AboutUs.Select(s => new { des = s.Description }); – athira Feb 08 '17 at 06:22
  • What is it you want to display - a collection or just one object? –  Feb 08 '17 at 06:24
  • Just one object , that i need to select the top 1 object from table – athira Feb 08 '17 at 06:26
  • 1
    If you want just one object, then its `var selectResult = db.AboutUs.FirstOrDefault();` (and read the dupe - do not use `var` until you understand what your doing) –  Feb 08 '17 at 06:26
  • But The error remains same, why is that occuring ?? – athira Feb 08 '17 at 06:30
  • sorry sir , it's working Thank you ! – athira Feb 08 '17 at 06:31
  • Then accept the duplicate to close this out –  Feb 08 '17 at 06:34
  • Its because my view, table and controllers name is same 's' is removed automatically from the table – athira Feb 08 '17 at 06:37

0 Answers0