mardi 28 mars 2017

The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.Survey_D8

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; }
    }
}

Aucun commentaire:

Enregistrer un commentaire