Reputation: 1
I want to get a List of items from Model to view, but i am not getting Exact Idea of That Please Help Me I am Adding my all class
Below
public class DivisionDTO
{
public Guid Id { get; set; }
public string Name { get; set; }
public List<GroupDTO> Groups { get; set; }
}
public class GroupDTO
{
public Guid Id { get; set; }
public string Name { get; set; }
public List<Team> Teams { get; set; }
}
public class Team
{
public int ID { get; set; }
public string TeamName { get; set; }
public int GroupId { get; set; }
public int DivisionId { get; set; }
public string pld { get; set; }
public string W { get; set; }
public string D { get; set; }
public string L { get; set; }
public string GF { get; set; }
public string GA { get; set; }
public string GD { get; set; }
public string Pts { get; set; }
}
The model itself is a list of items and every items contain List of child items as well. in DivisionDTO Class list GroupDTO values is there and in GroupDTO class Team value is there
My controller Code is following
public ActionResult EditGroup()
{
CreatingLeague.Models.DivisionDTO division = new CreatingLeague.Models.DivisionDTO();
division.Id = new Guid();
division.Name = "Div";
division.Groups = new List<CreatingLeague.Models.GroupDTO>();
CreatingLeague.Models.Team team = new Models.Team();
team.TeamName = "TeamA";
division.Groups.Add(new CreatingLeague.Models.GroupDTO() { Id = new Guid(), Name = "G1", Teams = new List<Team>() { team } });
return View(division);
}
I want to make a edit page can any one give me idea of that
Upvotes: 0
Views: 1762
Reputation: 9463
You can loop over the individual groups in the razor View.
@model DivisionDTO
@for (var i = 0; i < Model.Groups.Count(); i++) {
@Html.EditorFor(m => m.Groups[i].Name)
@for (var j = 0; j < Model.Groups[i].Teams.Count(); j++) {
@Html.EditorFor(m => m.Groups[i].Teams[j].TeamName)
}
}
Do not use foreach
, because the indices i
, j
are needed by the MVC modelbinder.
Your POST action can take the DivisonDto
as model, the child collections will have data.
[HttpPost]
public ActionResult Save(DivisonDto model) {
var firstGroupName = model.Groups[0].Name;
// ...
}
See Model Binding to a List MVC 4
Upvotes: 2