-1

I have an error. It is

The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.Survey_D8226C5F5E348399740EDE08FDF0A956BDAD893915272C075AF983B0C50DA25E', but this dictionary requires a model item of type 'SurveySpaceProject.Models.User'.

I am taking a Survey from database but it need User. I tried some solutions from Stackoverflow but there is no way. Can you help me please? It is not a homework. It is my project and I am started asp.net 1 week ago.

My View

       public ActionResult FillSurvey(int id)
        {
            var s = SSPEntity.getDB().Surveys.FirstOrDefault(x => x.ID == id);
            return View(s);
        }

My FillSurvey Page :

       @model SurveySpaceProject.Models.Survey

     @{
    ViewBag.Title = "FillSurvey";
    }

    <h2>FillSurvey</h2>

    <div>
    <h4>Survey</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.User.UserName)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.User.UserName)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Title)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Title)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.CreatedDate)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.CreatedDate)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.IsDeleted)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.IsDeleted)
        </dd>

       </dl>
      </div>
       <p>
        @Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
        @Html.ActionLink("Back to List", "Index")
      </p>

And My Survey Model

            namespace SurveySpaceProject.Models
         {
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    public partial class Survey
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Survey()
        {
            SurveyAnswers = new HashSet<SurveyAnswer>();
            SurveyQuestions = new HashSet<SurveyQuestion>();
            //SurveyUsers = new HashSet<SurveyUser>();
        }

        public int ID { get; set; }

        public int UserID { get; set; }

        [Required]
        [StringLength(150)]
        public string Title { get; set; }

        [Column(TypeName = "date")]
        public DateTime? CreatedDate { get; set; }

        public bool IsDeleted { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<SurveyAnswer> SurveyAnswers { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<SurveyQuestion> SurveyQuestions { get; set; }

        public virtual User User { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<SurveyUser> SurveyUsers { get; set; }
    }
}
Oya C.
  • 7
  • 1
  • Where are you getting the error? In which part of code exactly? – Suman Pathak Mar 28 '17 at 11:51
  • 1
    The view you posted uses `Survey` as its model. The only way you would get this particular exception is if the view that's actually being used has `User` as its model. In other words, you either aren't loading the view you think you are, or you haven't posted the code for the actual view that matters. – Chris Pratt Mar 28 '17 at 13:00
  • [This Q/A](http://stackoverflow.com/questions/40373595/the-model-item-passed-into-the-dictionary-is-of-type-but-this-dictionary-requ) explains the error, but you have not shown us the correct code (what you have shown will not generate that error). The error clearly states your calling a view which has `@model SurveySpaceProject.Models.User` (not `Survey` as you have shown) –  Mar 28 '17 at 22:19
  • If I pass anything but Survey, error states I need to call 'Survey'. But if i pass Survey as a model, it states that error. – Oya C. Mar 30 '17 at 06:11

1 Answers1

-1

You have 2 issues:

issue 1

The partial class and the model you are passing to the view are in namespace namespace SurveySpaceProject.Models but the Survey your query returns is in another namespace. So therefore they are different types. Make sure the nameplates are the same. This is what the error indicates.

Issue 2

You have to eagerly load the User property. Use Include like this:

var s = SSPEntity.getDB().Surveys.Include(x =>   
    x.User).FirstOrDefault(x => x.ID == id);

Now EF will generate a query so the User info is included in the query as well.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • 1
    This is incorrect on both counts. 1) The "other" namespace you're referring to is `System.Data.Entity.DynamicProxies`, which is where the dynamic proxies EF generates originate from. However, these proxy classes inherit from the entities they proxy for, so they can be used interchangeably. 2) You do not *have* to use `Include`. It may be more performant for the OP to include the related user in the initial query, but lazy-loading (i.e. not using `Include`) is a perfectly valid choice. – Chris Pratt Mar 28 '17 at 12:58
  • 1) the partial class has to have the same namespace as the created classes. 2) with lazy loading it will load the property if the query has not been executed. After FirstOrDefault, everything is done in memory so you do need `Include`. – CodingYoshi Mar 28 '17 at 15:22
  • 1
    Sorry, but again, you're wrong on both counts. Partial classes can be spread out in multiple namespaces. The compiler will collect them all as long as each namespace is referenced. On the second point, accessing the property will issue another query. That is how lazy loading works and it's the whole reason why EF creates proxy classes 8n the first place: so it can override the property to add this functionality. You don't have to Include first. – Chris Pratt Mar 28 '17 at 16:07
  • 1) Can you provide some proof about partial classes being in diff assemblies? I work with EF often and need to make the namespace the same. Read [this](http://stackoverflow.com/questions/4504288/partial-class-in-different-namespaces). 2) If you access the property after the query has been executed then EF will not issue another query. The only time it will do that is if you have an `IQueryable` and you keep executing that. `FirstOrDefault` does not return `IQueryable` so no EF will not send another query. After that you are working in memory. – CodingYoshi Mar 29 '17 at 00:31