如何发送多个模型进行查看?
How can I send multiple model to view?
我有两个实体 classes - Student
和 Mentor
。我想在一个视图中使用这两个模型。我将这两个模型组合在一个 class 中并发送到控制器中查看,但我无法达到值。我想像下面的视图代码一样使用 foreach 列出这些 classes 中的值。我该怎么做?
这是我的控制器代码
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);
}
这是Student
class
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; }
}
这是 Mentor
class:
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; }
}
}
这是组合 class 作为混合模型
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>
在您看来,使用 @model MixModel
而不是 @model IEnumerable<MixModel>
。
您的完整视图如下所示
@model MixModel
<div id="mentorr">
<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.mentors ) {
<tr>
<td></td>
<td></td>
<td></td>
</tr>
}
</tbody>
</table>
</div>
</div>
<div id="student">
<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.students ) {
<tr>
<td></td>
<td></td>
<td></td>
</tr>
}
</tbody>
</table>
</div>
</div>
我有两个实体 classes - Student
和 Mentor
。我想在一个视图中使用这两个模型。我将这两个模型组合在一个 class 中并发送到控制器中查看,但我无法达到值。我想像下面的视图代码一样使用 foreach 列出这些 classes 中的值。我该怎么做?
这是我的控制器代码
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);
}
这是Student
class
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; }
}
这是 Mentor
class:
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; }
}
}
这是组合 class 作为混合模型
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>
在您看来,使用 @model MixModel
而不是 @model IEnumerable<MixModel>
。
您的完整视图如下所示
@model MixModel
<div id="mentorr">
<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.mentors ) {
<tr>
<td></td>
<td></td>
<td></td>
</tr>
}
</tbody>
</table>
</div>
</div>
<div id="student">
<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.students ) {
<tr>
<td></td>
<td></td>
<td></td>
</tr>
}
</tbody>
</table>
</div>
</div>