Bumda
Bumda

Reputation: 29

How can I send multiple model to view?

I have two entity classes - Student and Mentor. I want to use this two models in one view. I combined these two models in a single class and send to view in controller but I cant reach values. I want to list the values in these classes with using foreach like below view code. How can I do this?

This is my controller code

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

This is the 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; }
}

And this is the 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; }
    }
}

This is combined class as mixmodel

namespace MentorShip.Models
{
    public class MixModel
    {
        public IEnumerable<Student> students { get; set; }
        public IEnumerable<Mentor> mentors { get; set; }
    }
}

This is my view

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

Upvotes: 0

Views: 150

Answers (1)

sisindrymedagam
sisindrymedagam

Reputation: 199

In your view, use @model MixModel instead of @model IEnumerable<MixModel>.

And your complete view looks like below

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

Upvotes: 1

Related Questions