Martacus
Martacus

Reputation: 725

List of objects is empty in Model after submitting form

In the cshtml file I am iterating over a list with objects and binding the values to textboxes. Here is my partial view.

@model BotelHotel.Models.ViewModels.BookingViewModel
<div>
    @using (Html.BeginForm("StepThree", "Book", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {

        @Html.LabelFor(model => model.BookingDate)<br />
        @Html.TextBoxFor(model => model.BookingDate, new { @class = "form-control datepicker", placeholder = "Datum Boeking", @readonly = true })

        @Html.LabelFor(model => model.AmountOfPersons)<br />
        @Html.TextBoxFor(model => model.AmountOfPersons, new { @class = "form-control", @readonly = true })<br />

        for (int i = 0; i < Model.persons.Count; i++)
        {
            @Html.LabelFor(model => model.persons[i].Name)<br />
            @Html.TextBoxFor(model => model.persons[i].Name, new { @class = "form-control"})<br />
        }

        <button class="btn btn-success">Submit</button>
    }
</div>

After clicking the submit button I have a breakpoint at the StepThree function. The list personsis empty. Any reason to why it is empty? The rest of stackoverflow seems to say that this is a good solution. But it does not work for me.

Here are my models.

public class BookingViewModel
    {
        public string step { get; set; }
        public int roomID { get; set; }
        public List<PersonViewModel> persons = new List<PersonViewModel>();

        public BookingViewModel()
        {

        }

        public BookingViewModel(int roomID)
        {
            this.roomID = roomID;
            this.step = "PartialOne";
        }

        [Required]
        [DisplayFormat(DataFormatString = "{0:dd MMM yyyy}")]
        [Display(Name = "Datum")]
        public DateTime BookingDate { get; set; }

        [Required]
        [Display(Name = "Aantal personen")]
        public int AmountOfPersons { get; set; }

    }
public class PersonViewModel
    {

        private Person _person;

        public PersonViewModel()
        {
            _person = new Person();
        }

        public PersonViewModel(Person person)
        {
            _person = person;
        }


        public int Id { get; set; }
        [Required]
        [Display(Name = "Naam")]
        public string Name { get; set; }
        [Required]
        [Display(Name = "Adres")]
        public string Address { get; set; }
        [Required]
        [Display(Name = "Email")]
        public string Email { get; set; }
        [Required]
        [Display(Name = "Post Code")]
        public string Postal_code { get; set; }

    }

And the method that is being posted to

        public ViewResult StepThree(BookingViewModel model)
        {
            model.step = "PartialThree";
            return View("Book", model);
        }

Upvotes: 0

Views: 536

Answers (1)

madoxdev
madoxdev

Reputation: 3880

try changing that to be a property rather than a field:

public List<PersonViewModel> persons {get;set;}

and the initilize it from the constructor:

persons = new List<PersonViewModel>();

Upvotes: 1

Related Questions