-1

I have this Model:

public class ClassRoom
{
    public List<Student> Students { get; set; }
}
public class Student
{
    public int ID { get; set; }
    public int Type { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
}

The values of the ID and Type are already full, I need to create a view where the student can add his name and last name. So I need to loop the list of students in the view.

I am doing the following:

@model Models.ClassRoom

@{
    ViewBag.Title = "Classroom";
}


@if (Model != null)
{
<form action="Controller/Method">
    foreach (var item in Model.Students)
    {
        @Html.HiddenFor(model => model.ID) 
        <div>
            @Html.TextBoxFor(model => model.Name)
            @Html.TextBoxFor(model => model.LastName)
        </div>

    }
    <input type="submit">
  </form>
  }

I want to eventually submit a model of type Classroom with a list of students filled with Name and Last Name for each ID

But this is not working.

How can I bind values From the View to item on a certain index in a list?

For each ID in the hidden input,I want to save the written name and last name.

Please help

I need to create a form and submit the ClassRoom with a full List of Students eventually. What should be the types in my Controller method and views?

HelpASisterOut
  • 3,085
  • 16
  • 45
  • 89
  • *But this is not working.* - Why? There is exception, something is not displayed? Give more detailed explanation about your expectations of how it should work. – 3615 Nov 30 '16 at 08:03
  • How do you send the new values to your controller ? – PMerlet Nov 30 '16 at 08:04
  • @Cubi Please check update on my question – HelpASisterOut Nov 30 '16 at 08:05
  • @3615 Can't bind a value to one certain index. there is no "ID" or "Name" in Model, they are in Model.Students – HelpASisterOut Nov 30 '16 at 08:05
  • @3615 it is not working before i run it. I cant bind values to certain indexes it seems. please check edit in my question for more info – HelpASisterOut Nov 30 '16 at 08:54
  • Ok, if I understand your problem correctly, you are trying to send the data back to the controller and it's always null. That's a common problem and have several ways to deal with, I've shown the easiest one. – 3615 Nov 30 '16 at 09:10
  • What do you mean _bind on a certain index in a list?_ - do you not want to bind all of them? –  Nov 30 '16 at 09:24
  • @StephenMuecke I do, but for each ID there should be a name and a last name assigned to that ID – HelpASisterOut Nov 30 '16 at 09:30
  • 1
    The see the answer by @3615. And for a more detailed explanation (and the preferred `EditorTemplate` method), refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) –  Nov 30 '16 at 09:32

1 Answers1

1

If you want to send your model back to controller, then you would need to generate naming correctly. There are several ways, one of them would look like this:

@using (Html.BeginForm("Index", "Home")) {
    <div style="margin-top: 100px;">
        @if (Model != null) {
            for (var i = 0; i <= Model.Students.Count - 1; i++) {
                @Html.HiddenFor(model => model.Students[i].ID)
                <div>
                    @Html.TextBoxFor(model => model.Students[i].Name)
                    @Html.TextBoxFor(model => model.Students[i].LastName)
                </div>
            }
        }
        <input type="submit" value="Go"/>
    </div>
}

And in controller dont forget to add:

[HttpGet]
public async Task<IActionResult> Index() {
    var classroom = new ClassRoom();
     ... //add some students to the classroom
    return View(classroom);
}

[HttpPost]
public async Task<IActionResult> Index(ClassRoom classRoom) {
    ...
}

Here can be found some more reading.

Community
  • 1
  • 1
3615
  • 3,787
  • 3
  • 20
  • 35
  • How are we binding every value to the item at the index? – HelpASisterOut Nov 30 '16 at 08:16
  • This is Sending the model ClassRoom to the Controller Method with List of Student Count = 0. Any idea why? – HelpASisterOut Nov 30 '16 at 09:36
  • @HelpASisterOut This suppose to *edit* the students, does your model have any student's in the first place? – 3615 Nov 30 '16 at 09:39
  • @HelpASisterOut, If your collection contains `Student` objects when the view is generated, then they will be correctly bound. –  Nov 30 '16 at 09:39
  • @StephenMuecke The collection contains Student objects, it is correctly filling the ID values in the Hidden inputs, but when I click submit, the list of students comes back empty.. – HelpASisterOut Nov 30 '16 at 09:48
  • @HelpASisterOut No it won't (if you have correctly implemented this code as is). –  Nov 30 '16 at 09:51
  • Worked. Your code is missing an @HTML.EditorFor, I am gonna add it to your answer. – HelpASisterOut Nov 30 '16 at 09:54
  • 1
    @HelpASisterOut, I rejected you edit (and I hope others will too) - adding `EditorFor(m => m.Students)` will just repeat the inputs again depending on the template (and it would be pointless since the `DefaultModelBinder` will only bind the values generated by the controls in the `for` loop and ignore those generated by `EditorFor()` –  Nov 30 '16 at 10:00