【发布时间】:2021-01-13 16:14:28
【问题描述】:
我有两个实体类,学生和导师。我想在一个视图中使用这两个模型。我将这两个模型组合在一个类中并发送到控制器中的视图,但我无法达到值。我想使用 foreach 列出这些类中的值,如下图所示。我该怎么做。
这是我的控制器代码
MixModel m = new MixModel();
public ActionResult Main(string user)
{
ViewBag.message = user;
var mentor = from r in m.mentors
select r;
var student = from r in m.students
select r;
var model = new MixModel { mentors = mentor.ToList(), students=student.ToList() };
return View(model);
}
这是学生课
public class Student
{
[Key]
public int Id { get; set; }
public string Ad { get; set; }
public string Soyad { get; set; }
public string Departmant { get; set; }
public string Email { get; set; }
public string Parola { get; set; }
}
这是导师班
namespace MentorShip.Models
{
public class Mentor
{
[Key]
public int Id { get; set; }
public string Ad { get; set; }
public string Soyad { get; set; }
public string Departmant { get; set; }
public string Job { get; set; }
public string Email { get; set; }
public string Parola { get; set; }
}
}
这是混合模型的组合类
namespace MentorShip.Models
{
public class MixModel
{
public IEnumerable<Student> students { get; set; }
public IEnumerable<Mentor> mentors { get; set; }
}
}
这是我的看法
@model IEnumerable<MixModel>
<div id="mentorr" style="display:none;margin-left:20%;">
<div class="container">
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>Ad</th>
<th>Soyad</th>
<th>Meslek</th>
</tr>
</thead>
<tbody>
@foreach(var deger in Model )
{
<tr>
<td></td>
<td></td>
<td></td>
</tr>
}
</tbody>
</table>
</div>
</div>
【问题讨论】:
标签: asp.net-core model-view-controller view controller asp.net-core-mvc