I try to implement student attendance application where instructor can give the student one of three status(Present, Absent, Unrecorded). After instructor login he can go to class and change each student status and save change
So simply I modify the https://stackoverflow.com/a/18859712/3254920
Models
public class Student
{
public string Id { get; set; }
[Display(Name = "Full Name ")]
public string FullName { get; set; }
[Display(Name = "Student Status")]
public int StudentStatus { get; set; }
public List<StudentStatus> StudentStatusList { get; set; }
}
public class StudentStatus
{
public string ID { get; set; }
public string Type { get; set; }
}
My controller
public ActionResult Index()
{
List<Student> model=new List<Student>();
for (int i = 0; i < 5; i++)
{
var student = new Student
{
Id = i.ToString(),
FullName = "Asd" +i,
StudentStatusList = new List<StudentStatus>
{
new StudentStatus{ID="1" , Type = "Present"},
new StudentStatus{ID="2" , Type = "Absent"},
new StudentStatus{ID="3" , Type = "Unrecorded"}
}
};
model.Add(student);
}
return View(model);
}
[HttpPost]
public ActionResult Index(List<Student> model)
{
if (ModelState.IsValid)
{
//TODO: Save your model and redirect
}
return View(model);
}
The View
@model List<Models.Student>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<table class="table table-hover table-condensed table-striped table-bordered">
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().FullName)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().StudentStatus)
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.FullName)
</td>
<td>
@foreach (var status in item.StudentStatusList)
{
@Html.RadioButtonFor(modelItem => item.StudentStatus, status.ID, new { @id = "Status" + item.Id+ status.ID, @Name = item.Id })
@Html.Label("Status" + item.Id+ status.ID, status.Type)
}
</td>
</tr>
}
</table>
<input type="submit" value="Save" class="btn btn-default" />
}
After I run the application I found that the model is null and I don't know why
I appreciate any help.
Edit
I have also tried the following and the model still null
@model List<Models.Student>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<table class="table table-hover table-condensed table-striped table-bordered">
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().FullName)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().StudentStatus)
</th>
</tr>
@for(int i = 0; i < Model.Count(); i++)
{
<tr>
<td>
@Html.DisplayFor(m=>m[i].FullName)
</td>
<td>
@foreach (var status in Model[i].StudentStatusList)
{
<label>
@Html.RadioButtonFor(m => m[i].StudentStatus, status.ID, new { id = "" })
<span>@status.Type</span>
</label>
}
</td>
</tr>
}
</table>
<input type="submit" value="Save" class="btn btn-default" />
}
Edit 2
the application after execute
Edit 3
Thanks Stephen Muecke For your help I just restart my computer and every things work correctly