2

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

1 Answer 1

1

try to replace foreach loop by for loop

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

and the controller action

public ActionResult IndexViewModel(ViewModel viewModel)  
    {           
       ....  
    }
Sign up to request clarification or add additional context in comments.

1 Comment

many thanks for the help. solved the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.