1

I am new in MVC and I need your help I get this error

The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery'1[name space.Models.ViewModelLevelSchedule]', but this dictionary requires a model item of type 'name space.Models.ViewModelLevelSchedule'.

and i don't know how to fix it

my view model is here

public class ViewModelLevelSchedule
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string BookName { get; set; }
    public string Description { get; set; }
    public string Picture { get; set; }
    public decimal Price { get; set; }
    public string  Level { get; set; }
    public Boolean Visibile { get; set; }
}

and this is my controller

ViewModelLevelSchedule _details = new ViewModelLevelSchedule();
var sch = from s in db.Schedules
          join _lev in db.Levels on s.Level equals _lev.ID
          where (s.ID == id && s.Visibile==true) 
                     select new ViewModelLevelSchedule
                     {
                         Level=_lev.Name,
                         BookName=s.BookName,
                         Title=s.Title,
                         Description=s.Description,
                         Picture=s.Picture,
                         Price=s.Price
                     };
    if (sch== null)
    {
        return HttpNotFound();
    }
    return View(sch);
}

and then end it is my view page

@model nameSpace.Models.ViewModelLevelSchedule

<!-- Page Title -->
<div class="section section-breadcrumbs">
    <div class="container">
        <div class="row">
            <div class="col-md-12">
                @Html.DisplayFor(model => model.)
            </div>
        </div>
    </div>
</div>

<div class="section">
<div class="container">
    <div class="row">
        <!-- Product Image & Available Colors -->
        <div class="col-sm-6">
            <div class="product-image-large">
                <img       src="@Url.Content(String.Format("~/Images/Schedule/{0}", Model.Picture))" alt="avatar" />
            </div>
        </div>
        <!-- Product Summary & Options -->
        <div class="col-sm-6 product-details">
            @Html.DisplayFor(model => model.BookName)
            <div class="price">
                <span class="price-was">$959.99</span>
                 @Html.DisplayFor(model => model.Price)ریال
            </div>
            <table class="shop-item-selections">
                <!-- Quantity -->
                <tr>
                    <td><b>سطح:</b></td>
                    <td>
                        @Html.DisplayFor(model => model.Level)
                    </td>
                </tr>
            </table>
            <h5>توضیحات</h5>
            <p>
                @Html.DisplayFor(model => model.Description)
            </p>
        </div>
        <!-- End Product Summary & Options -->
        <!-- Full Description & Specification -->
                </div>
            </div>
        </div> 
        <!-- End Full Description & Specification -->

if you want to see my models

public class Schedule
{
    [Key]
    [Required]
    public int ID { get; set; }
    public string Title { get; set; }
    public string BookName { get; set; }
    public string Description { get; set; }
    public string Picture { get; set; }
    public decimal Price { get; set; }
    public int Level { get; set; }
    public Boolean Visibile { get; set; }
}


public class Level
{
    [Key]
    [Required]
    public int ID { get; set; }
    public string Name { get; set; }
}
marjan_gh
  • 17
  • 2
  • Have you tried building the solution first? – Bojje Jan 19 '17 at 15:19
  • Your query returns a collection of `ViewModelLevelSchedule` (even though it may contain only one item) but your view expects a single `ViewModelLevelSchedule` (use `.FirstOrDefault()`) –  Jan 19 '17 at 23:59

0 Answers0