C# 传入字典的模型项的类型为';System.Collections.Generic.List`1[]和#x27;,但本词典需要类型为'';
以下是我对viewModel的看法:-C# 传入字典的模型项的类型为';System.Collections.Generic.List`1[]和#x27;,但本词典需要类型为'';,c#,asp.net-mvc,C#,Asp.net Mvc,以下是我对viewModel的看法:- @model _24By7CallAndBPOPvtLtd.Models.JobDetailandApplyModel <table class="table table-striped"> @foreach (var item in Model.jobdetail)
@model _24By7CallAndBPOPvtLtd.Models.JobDetailandApplyModel
<table class="table table-striped">
@foreach (var item in Model.jobdetail)
{
<tr>
<td>
@item.job_title
</td>
<td>
<a href="@Url.Action("Edit", new { id = item.Job_ID })"><i class="glyphicon glyphicon-pencil"></i></a>
<a href="@Url.Action("Delete", new { id = item.Job_ID })"><i class="glyphicon glyphicon-remove" style="color:red"></i></a>
</td>
</tr>
}
</table>
<table class="table table-striped">
@foreach (var item in Model.jobapplied)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.User_Account.Full_Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Job_Det.job_title)
</td>
<td>
<a href="/Content/CV/@item.User_CV.Cv_Path" target="_blank">Download</a>
</td>
</tr>
}
</table>
public class JobDetailandApplyModel
{
public IEnumerable<Job_Det> jobdetail { get; set; }
public IEnumerable<Job_Applied> jobapplied { get; set; }
}
public partial class Job_Applied
{
public int JobApplied_ID { get; set; }
public int Employeer_ID { get; set; }
public int Job_ID { get; set; }
public int User_ID { get; set; }
public Nullable<int> Cv_Id { get; set; }
public Nullable<int> status_Id { get; set; }
public System.DateTime date_applied { get; set; }
public Nullable<System.TimeSpan> time_applied { get; set; }
public string timestamp_applied { get; set; }
public virtual Employeer Employeer { get; set; }
public virtual Job_Det Job_Det { get; set; }
public virtual Status Status { get; set; }
public virtual User_CV User_CV { get; set; }
public virtual User_Account User_Account { get; set; }
}
这是我的ViewModel:-
@model _24By7CallAndBPOPvtLtd.Models.JobDetailandApplyModel
<table class="table table-striped">
@foreach (var item in Model.jobdetail)
{
<tr>
<td>
@item.job_title
</td>
<td>
<a href="@Url.Action("Edit", new { id = item.Job_ID })"><i class="glyphicon glyphicon-pencil"></i></a>
<a href="@Url.Action("Delete", new { id = item.Job_ID })"><i class="glyphicon glyphicon-remove" style="color:red"></i></a>
</td>
</tr>
}
</table>
<table class="table table-striped">
@foreach (var item in Model.jobapplied)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.User_Account.Full_Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Job_Det.job_title)
</td>
<td>
<a href="/Content/CV/@item.User_CV.Cv_Path" target="_blank">Download</a>
</td>
</tr>
}
</table>
public class JobDetailandApplyModel
{
public IEnumerable<Job_Det> jobdetail { get; set; }
public IEnumerable<Job_Applied> jobapplied { get; set; }
}
public partial class Job_Applied
{
public int JobApplied_ID { get; set; }
public int Employeer_ID { get; set; }
public int Job_ID { get; set; }
public int User_ID { get; set; }
public Nullable<int> Cv_Id { get; set; }
public Nullable<int> status_Id { get; set; }
public System.DateTime date_applied { get; set; }
public Nullable<System.TimeSpan> time_applied { get; set; }
public string timestamp_applied { get; set; }
public virtual Employeer Employeer { get; set; }
public virtual Job_Det Job_Det { get; set; }
public virtual Status Status { get; set; }
public virtual User_CV User_CV { get; set; }
public virtual User_Account User_Account { get; set; }
}
公共类作业详细信息和应用模型
{
公共IEnumerable作业详细信息{get;set;}
公共IEnumerable作业应用{get;set;}
}
申请公共部分课程
{
public int JobApplied_ID{get;set;}
public int Employeer_ID{get;set;}
public int Job_ID{get;set;}
公共int用户_ID{get;set;}
公共可为空的Cv_Id{get;set;}
公共可空状态_Id{get;set;}
public System.DateTime date_应用{get;set;}
公共可空时间_应用{get;set;}
应用的公共字符串时间戳{get;set;}
公共虚拟雇员{get;set;}
公共虚拟作业\u Det作业\u Det{get;set;}
公共虚拟状态状态{get;set;}
公共虚拟用户\u CV User\u CV{get;set;}
公共虚拟用户\u帐户用户\u帐户{get;set;}
}
这段代码有什么问题?我正在使用viewModel在一个视图中传递两个模型,我需要在一个视图中显示两个列表。试试看
var jobs = db.Job_Det.ToList();
var jobApplied = db.Job_Applied.Include(x => x.Job_Det).ToList();
JobDetailandApplyModel model = new JobDetailandApplyModel() {
jobdetail = jobs,
jobapplied = jobApplied
}
return View(model);
此外,不确定这是否会产生影响,但需要替换
@Html.DisplayFor(modelItem => item.Job_Det.job_title)
与
您需要正确初始化模型,并为两个属性中的每个属性指定值。将控制器更改为类似以下内容:
var model = new JobDetailAndApplyModel();
model.jobdetail = db.Job_Det.ToList();
model.jobapplied = db.Job.Applied.ToList();
return view(model);
编辑:您拥有的Test2控制器不会做很多事情,当您向它传递这样一个字符串时,它只使用与该字符串同名的视图。它没有传递任何模型,因此您将得到一个空引用错误。你需要这样的东西才能让它工作:
return View("Test1", model);
使用上面的其他代码创建模型对象。您的视图需要JobDetails和ApplyModel类型的模型 Test1提供了一个类型为 列表
Test2不提供任何模型您试图将列表分配给整个模型。您需要初始化模型,并且他们为2个列表中的每一个分配值。您没有在
Test2
方法中将列表传递给视图。这或多或少就是我所说的;)是的,看起来我们两个在同一时间打字。它说空引用在哪一行吗?我认为有一些数据丢失了,因为有很多隐含对象存在,没有空检查。每个JobApplied是否都有一个Job_Det和UserAccount项目作为子项?当我放置调试器时,它成功地从控制器传递并显示每个项目的计数,当传递到视图时,它在@Html.DisplayFor(modelItem=>item.Job_Det.Job_title)中提供空引用Job_Det表是否链接到Job_Applied表?如果没有,您可以尝试@Html.DisplayFor(modelItem=>item.job\u title)
。如果看不到更多代码,就很难进行诊断。行中的对象引用未设置为对象的实例@Html.DisplayFor(modelItem=>item.Job\u Det.Job\u title)你确定你的数据库正在返回数据吗?我试过了。但还是同样的问题,请记住我在一个方法Test1中使用了你的代码,我对另一个方法test2进行了注释。你能把这两个类的代码放在问题中吗?你的意思是什么?我不明白