如何在 mvc 中使用 ViewModel 将多个列表从视图获取到控制器

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

你能帮我吗,使用视图模型从视图控制器中获取多个 table 数据作为列表。下面是我的代码,它一直在工作,直到将多个模型传递给视图。我无法从控制器操作方法中的视图获取这些更新值以更新数据。

我有两个模型。

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

将模型中的那些模型作为列表传递

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

在控制器中,我已经定义了视图模型

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

在我看来,我的所有值都是正确的,但是当我更改值时仍然提交。更新的数据无法获取操作方法。请帮助我。

@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" />
}

这里我无法从视图中获取列表

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

尝试用for循环替换foreach循环

@for (var i=0; i <  Model.Teachers.Count; i+=1)  
    {  
        <tr>  
            <td>@Html.DisplayFor(model =>model.Teachers[i].TeacherId)</td>  
            <td>@Html.EditorFor(model => model.Teachers[i].Code)</td>  
            <td>@Html.EditorFor(model => model.Teachers[i].Name)</td>  
        </tr>  
    }  

@for (var i=0; i <  Model.Students.Count; i+=1)  
    {  
        <tr>  
            <td>@Html.DisplayFor(model => model.Students[i].StudentId)</td>  
            <td>@Html.EditorFor(model =>  model.Students[i].Code)</td>  
            <td>@Html.EditorFor(model =>  model.Students[i].Name)</td>   
        </tr>  
    }  

和控制器动作

public ActionResult IndexViewModel(ViewModel viewModel)  
    {           
       ....  
    }