'How to get multiple lists from view to Controller using ViewModel in mvc

Could you please help me, To get multiple table data as list from view controller using view model. Below is my Code Its Working till passing multiple model to View. I am not able to get those updated values from View in controller Action Method to Update the data.

I have Two Models.

public class Teacher  
{  
    public int TeacherId { get; set; }  
    public string Code { get; set; }  
    public string Name { get; set; }  
}   
  
public class Student  
{  
    public int StudentId { get; set; }  
    public string Code { get; set; }  
    public string Name { get; set; }          
}

Pass those Model in a model as list

public class ViewModel  
{  
    public IEnumerable<Teacher> Teachers { get; set; }  
    public IEnumerable<Student> Students { get; set; }  
}  

In Controller, I have define the View Model

[HttpGet]
public ActionResult IndexViewModel()  
    {  
        ViewModel mymodel = new ViewModel();  
        mymodel.Teachers = GetTeachers();  
        mymodel.Students = GetStudents();  
        return View(mymodel);  
    }

In My View, I have all the values correctly but when I change the value same submit. the updated data is not able to get on action method. Please Help me on this.

@using MultipleModelInOneView;  
@model ViewModel   
@{  
    ViewBag.Title = "Home Page";  
}  
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
<table>  
    <tr>  
        <th>Id</th>  
        <th>Code</th>  
        <th>Name</th>  
    </tr>  
    @foreach (Teacher teacher in Model.Teachers)  
    {  
        <tr>  
            <td>@Html.DisplayFor(model => @teacher.TeacherId)</td>  
            <td>@Html.EditorFor(model => @teacher.Code)</td>  
            <td>@Html.EditorFor(model => @teacher.Name)</td>  
        </tr>  
    }  
</table>  
   
<p><b>Student List</b></p>  
   
<table>  
    <tr>  
        <th>Id</th>  
        <th>Code</th>  
        <th>Name</th>  
        <th>Enrollment No</th>  
    </tr>  
    @foreach (Student student in Model.Students)  
    {  
        <tr>  
            <td>@Html.DisplayFor(model => @student.StudentId)</td>  
            <td>@Html.EditorFor(model => @student.Code)</td>  
            <td>@Html.EditorFor(model => @student.Name)</td>   
        </tr>  
    }  
</table> 

<input type="submit" value="Save" class="btn btn-primary" />
}

Here I am Not able to Get List from View

[HttpPost]
public ActionResult IndexViewModel("Here I'm not able to Get List from View")  
    {           
        return View();  
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source