0

Whenever i m passing data from Controller to View this error is showing

Server Error in '/' Application.
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[<>f__AnonymousType1`6[System.String,System.String,System.Nullable`1[System.DateTime],System.Nullable`1[System.DateTime],System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IList`1[CaliberCoaching.Models.CareerInformation]'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[<>f__AnonymousType1`6[System.String,System.String,System.Nullable`1[System.DateTime],System.Nullable`1[System.DateTime],System.String,System.String]]', but this dictionary requires a model item of type 'System.Collections.Generic.IList`1[CaliberCoaching.Models.CareerInformation]'.

This is My Controller Code

 public ActionResult JobList()
        {
            CaliberCoachingContext objCaliberCoachingContext = new CaliberCoachingContext();
            var lstCareerInformation = (from job in objCaliberCoachingContext.CareerInformations
                                                            select new { job.NameOfPost, job.PostName, job.StartDate, job.LastDate, job.Eligibility, job.NoOfVacancies }).ToList();

            return View(lstCareerInformation);
        }

and here is my view

@model IEnumerable<CaliberCoaching.Models.CareerInformation>
@{
    ViewBag.Title = "JobList";
    Layout = "~/Views/Shared/_CommonLayout.cshtml";
}
@foreach (var item in Model)
{
    <div class="single-item-wrapper">

        <div class="courses-content-wrapper">
            <h3 class="title-default-left-bold">@item.NameOfPost</h3>
        </div>
    </div>
}

Please Give me the solution for this Problem.

1 Answer 1

1

The exception is self explanatory! Your razor view is strongly typed to a collection of CareerInformation objects, but from your action method, you are passing a different type to the view!

In your action method, the variable lstCareerInformation is not a collection of CareerInformation objects, but a collection of anonymous objects. This is happening because your LINQ expression is doing a projection to an annonymous object.

 select new { job.NameOfPost, job.PostName, job.StartDate,
              job.LastDate, job.Eligibility, job.NoOfVacancies }

select new will create an anonymous object

To fix the error, you should return a collection of CareerInformation objects. Just remove the projection part from your LINQ expression.

public ActionResult JobList()
{
    var db = new CaliberCoachingContext();
    var careerInformationList = db.CareerInformations.ToList();
    return View(careerInformationList);
}

EDIT : As per the comment

i have used anonymous object because i wants to select particular columns instead of all the columns from CareerInformation properties.

Then you should be using a view model. Create a viewmodel class with properties needed in your view.

public class JobVm
{
   public string PostName { set;get;}
   public Eligibility { set;get;}
   public DateTime StartDate { set;get;}
   // Add other properties needed by the view 
}

Now in your action method, project create view model objects from your entity object collection using the Select method.

public ActionResult JobList()
{
    var db = new CaliberCoachingContext();
    var careerInformationList = db.CareerInformations
                                  .Select(a=> new JobVm { PostName = a.PostName,
                                                          StartDate = a.StartDate,
                                                          Eligibility = a.Eligibility })
                                  .ToList();
    return View(careerInformationList);
}

Here careerInformationList is a list of JobVm objects and that is what we are passing to the View. So make sure your view is strongly typed to a collection of the JobVm objects.

@model List<JobVm>
<h2>Jobs</h2>
@foreach(var job in Model)
{
   <div>@job.PostName</div>
   <p>@job.Eligibility</p>
}
Sign up to request clarification or add additional context in comments.

2 Comments

i have used anonymous object because i wants to select particular columns instead of all the columns from CareerInformation properties.
Then you should be using a view model. See the updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.