MVC Razor View: How to render a list of text boxes for a List in the model?

前端 未结 2 1025
北荒
北荒 2021-01-20 10:41

I have a List in my model. This list contains 2 tasks(say) in it

public List Tasks { get; set; }

public class Task    {         


        
2条回答
  •  难免孤独
    2021-01-20 11:37

    In case you want to have multiple elements with textboxes.

     for (int i = 0; i < Model.Students.Count; i++)
                                {
                                    @Html.HiddenFor(modelIem => Model.Students[i].StudentId)
    
                                    
                                        
                                            @Html.DisplayFor(modelItem => Model.Students[i].Student.FirstNames)
                                        
                                        
                                            @Html.DisplayFor(modelItem => Model.Students[i].Student.LastNames)
                                        
                                        
                                            @Html.TextBoxFor(modelItem => Model.Students[i].Score, new { @type = "number" })
                                        
     }
    

    This is the ViewModel

    public class MyModel 
    {
     public List Students { get; set; }
        }
    
        public class StudentGrade {
            public ApplicationUser Student { get; set; }
            [Range(1, 100)]
            public int? Score { get; set; } = null;
            public string Description { get; set; } = null;
            public string StudentId { get; set; }
        }
    

    At the End it will look like this.

提交回复
热议问题