still now i generate html table like below code example but interested to know how could i generate html table using editorfor using editor template
see my full razor and view model code and guide me how to achieve my goal.
@model MVCCRUDPageList.Models.StudentListViewModel
@{
ViewBag.Title = "Index";
}
<h2>CREATE TABULAR UI WITH HTML TABLE</h2>
@using (Html.BeginForm("Index", "HtmlTable", FormMethod.Post))
{
<div class="form-group">
<div class="col-md-12 table-responsive">
<table class="table table-bordered table-hover">
<tr>
<th>
Row No
</th>
<th>
ID
</th>
<th>
Name
</th>
<th>
Country
</th>
<th>
Hobbies
</th>
<th>
Sex
</th>
</tr>
}
@for (int x=0; x<=Model.Students.Count-1;x++)
{
<tr>
<td>
<label>@(x+1)</label>
</td>
<td>
@Html.TextBoxFor(m => m.Students[x].ID)
</td>
<td>
@Html.TextBoxFor(m => m.Students[x].Name)
</td>
<td>
@Html.DropDownListFor(m => m.Students[x].CountryID,
new SelectList(Model.Country, "ID", "Name", Model.Students[x].CountryID),
"-- Select Countries--", new { id = "cboCountry", @class = "edit-mode" })
</td>
<td>
@for (var i = 0; i < Model.Students.FirstOrDefault().Hobbies.Count; i++)
{
<div class="checkbox">
@Html.HiddenFor(m => m.Students[x].Hobbies[i].ID)
@Html.HiddenFor(m => m.Students[x].Hobbies[i].Name)
@Html.CheckBoxFor(m => m.Students[x].Hobbies[i].Checked)
@Html.LabelFor(m => m.Students[x].Hobbies[i].Name, Model.Students[x].Hobbies[i].Name)
</div>
}
</td>
<td>
@for (var i = 0; i < Model.Sex.Count; i++)
{
<div class="checkbox">
@Html.HiddenFor(m => Model.Sex[i].ID)
@Html.HiddenFor(m => Model.Sex[i].SexName)
@Html.RadioButtonFor(m => m.Students[x].SexID, Model.Sex[i].ID)
@Html.LabelFor(m => m.Students[x].SexID, Model.Sex[i].SexName)
</div>
}
</td>
</tr>
}
</table>
</div>
<input type="submit" value="Submit" />
</div>
}
View Model Code
public class StudentListViewModel
{
public IList<Student> Students { get; set; }
public List<Country> Country { get; set; }
public List<Sex> Sex { get; set; }
public StudentListViewModel()
{
Students = new List<Student>
{
new Student
{
ID=1,Name="Keith",CountryID=0,SexID="F",
Hobbies= new List<Hobby>
{
new Hobby{ID=1,Name="Football",Checked=false},
new Hobby{ID=2,Name="Hocky",Checked=false},
new Hobby{ID=3,Name="Cricket",Checked=false}
}
},
new Student
{
ID=2,Name="Paul",CountryID=2,
Hobbies= new List<Hobby>
{
new Hobby{ID=1,Name="Football",Checked=false},
new Hobby{ID=2,Name="Hocky",Checked=false},
new Hobby{ID=3,Name="Cricket",Checked=false}
}
},
new Student
{
ID=3,Name="Sam",CountryID=3,
Hobbies= new List<Hobby>
{
new Hobby{ID=1,Name="Football",Checked=false},
new Hobby{ID=2,Name="Hocky",Checked=false},
new Hobby{ID=3,Name="Cricket",Checked=false}
}
}
};
Country = new List<Country>
{
new Country{ID=1,Name="India"},
new Country{ID=2,Name="UK"},
new Country{ID=3,Name="USA"}
};
Sex = new List<Sex>
{
new Sex{ID="M",SexName="Male"},
new Sex{ID="F",SexName="Female"}
};
}
}
please guide me how to restructure my razor code to use editorfor and editor template for clean code. what will be name of editor template ?
thanks
See Question&Answers more detail:
os