简体   繁体   中英

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List`

Model: StudentData (Model 1)

namespace Aug16.Models
{
    [Table("Stdnt_Info")]
    public class StudentData
    {
        [Key]
        public long Stdnt_Id { get; set; }

        public string Stdnt_Name { get; set; }
        public string Stdnt_Fname { get; set; }
        public string Stdnt_Address { get; set; }

        public string Stdnt_Semmester { get; set; }
        public DateTime Sem_StartDate { get; set; }
        public DateTime Sem_EndDate { get; set; }

        public int Stdnt_Mark1 { get; set; }
        public int Stdnt_Mark2 { get; set; }
        public int Stdnt_Mark3 { get; set; }
        public Decimal Stdnt_Sem_Per { get; set; }
    }
}
-------------------------------------------------------------------------------

DBContext Model: Aug16Data (Model2)

namespace Aug16.Models
{
    public class Stdnt_Details : DbContext
    { 
        public DbSet<StudentData> Student_Information { get; set; }
        //public DbSet<Stdnt_Details> Student_Information { get; set; }
        //public DbSet<Aug16Data> Stdnt_Mark { get; set; }
    }
}
------------------------------------------------------------------------------

Controller : StudentDataController

namespace Aug16.Controllers
{
    public class StudentDataController : Controller
    {
        //
        // GET: /StudentData/

        Aug16.Models.Stdnt_Details StoreDB = new Models.Stdnt_Details();

        public ActionResult Aug16DataAction()
        {
            var StdDet = StoreDB.Student_Information.ToList();
            return View(StdDet);
        }
    }
}
---------------------------------------------------------------------------

View: Aug16DataAction

@model IEnumerable<Aug16.Models.Stdnt_Details>

@{
    ViewBag.Title = "Aug16DataAction";
}

<h2>Aug16DataAction</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

</table>

Error as below

Server Error in '/' Application.

The model item passed into the dictionary is of type 'System.Collections.Generic.List[Aug16.Models.StudentData]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable[Aug16.Models.Stdnt_Details]'.

Please what is the solution for this error?

First, you need to change your model type, as mentioned on comments to your question.

However, you are looking an empty view because you don't have the code to show all the model properties. Try to change your view with this code:

@model IEnumerable<Test.Models.StudentData>

@{
    ViewBag.Title = "Aug16DataAction";
}

<h2>Aug16DataAction</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Stdnt_Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Stdnt_Fname)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Stdnt_Address)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Stdnt_Semmester)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Sem_StartDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Sem_EndDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Stdnt_Mark1)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Stdnt_Mark2)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Stdnt_Mark3)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Stdnt_Sem_Per)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Stdnt_Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Stdnt_Fname)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Stdnt_Address)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Stdnt_Semmester)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Sem_StartDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Sem_EndDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Stdnt_Mark1)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Stdnt_Mark2)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Stdnt_Mark3)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Stdnt_Sem_Per)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.Stdnt_Id }) |
            @Html.ActionLink("Details", "Details", new { id = item.Stdnt_Id }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.Stdnt_Id })
            </td>
        </tr>
    }

</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM