繁体   English   中英

从控制器传递对象集合到View Asp.Net MVC

[英]Pass Collection of Object from Controller to View Asp.Net MVC

每当我将数据从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>
}

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

唯一的例外是自我解释! 剃刀视图的类型强烈地指向了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);
}

这里careerInformationList是列表JobVm对象,这就是我们传递给视图。 因此,请确保将视图强类型JobVm对象的集合。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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