0

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 enter image description here

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

enter image description here

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 enter image description here

Edit 3

Thanks Stephen Muecke For your help I just restart my computer and every things work correctly

Community
  • 1
  • 1
Abdul Hadi
  • 1,229
  • 1
  • 11
  • 20
  • You need to change the outer `foreach` loop (iterating each `Student`) to a `for` loop or use a custom `EditorTemplate` as explained in the dupe. –  Feb 17 '16 at 21:19
  • You should also consider a view model containing `List` and `List` so you have only one copy of status list - `foreach (status in Model.StudentStatusList) { }` And NEVER set the `Name` attribute when using the HtmlHelpers –  Feb 17 '16 at 21:26
  • Dear Stephen I change the view to @for(int i = 0; i < Model.Count; i++) { @Html.DisplayFor(m=>m[i].FullName) @foreach (var status in Model[i].StudentStatusList) { } } and still get null – Abdul Hadi Feb 17 '16 at 21:52
  • Too hard to read your code in comments (especially when its not formatted). Edit you question and append the revised view code to the bottom and I'll have a look. –  Feb 17 '16 at 21:55
  • Okay I edit the question – Abdul Hadi Feb 17 '16 at 21:59
  • You cannot change the original question. I have rolled back the changes and **appended** the new code. –  Feb 17 '16 at 22:43
  • The revised code you have shown works fine. The `model` parameter will not be `null` (although the `FullName` property will be because you have not rendered a control for it –  Feb 17 '16 at 22:47
  • It doesn't work when click submit the revived model is null, I solve the problem if I use FormCollection instead of List and I don't know why it doesn't work if use list? – Abdul Hadi Feb 17 '16 at 22:51
  • Then you have done something else wrong that you have not shown us. But the image you have shown for the view does not match your code - the table headings would be displayed as "Full Name" and "Student Status" (not `"Student Name" and "Status") so I assume you have not shown the correct code –  Feb 17 '16 at 22:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103777/discussion-between-abdul-hadi-and-stephen-muecke). – Abdul Hadi Feb 17 '16 at 22:58

0 Answers0