从Controller传递对象集合以查看Asp.Net MVC

时间:2018-04-28 18:06:50

标签: c# asp.net-mvc entity-framework razor

每当我将数据从Controller传递给View时,此错误都会显示

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]'.

这是我的控制器代码

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

这是我的观点

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

请给我解决这个问题的方法。

1 个答案:

答案 0 :(得分:0)

例外是自我解释!您的剃刀视图是强类型的CareerInformation个对象的集合,但是从您的操作方法,您将不同的类型传递给视图!

在您的操作方法中,变量lstCareerInformation不是CareerInformation个对象的集合,而是匿名对象的集合。这是因为您的LINQ表达式正在对一个匿名对象进行投影。

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

select new将创建一个匿名对象

要修复错误,您应该返回CareerInformation个对象的集合。只需从LINQ表达式中删除投影部分。

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

编辑:根据评论

  

我使用了匿名对象,因为我想选择特定对象   列而不是CareerInformation属性中的所有列。

然后你应该使用视图模型。创建一个viewmodel类,其中包含视图中所需的属性。

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

现在,在您的操作方法中,项目使用Select方法从实体对象集合创建视图模型对象。

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

此处careerInformationListJobVm个对象的列表,这就是我们传递给View的内容。因此,请确保您的视图强烈输入JobVm个对象的集合。

@model List<JobVm>
<h2>Jobs</h2>
@foreach(var job in Model)
{
   <div>@job.PostName</div>
   <p>@job.Eligibility</p>
}